Why parentheses are important in function pointer declaration?
Asked Answered
T

3

10

I don't understand why the declaration below is accepted:

typedef void    (*_tStandardDeclaration)(LPVOID);

while the following doesn't:

typedef void    *_tDeclarationWithoutParenthesis(LPVOID);
typedef void*   _tAlternateDeclaration(LPVOID);

I am using MSVC6 (I know it's obsolete and non-standard, but it's needed to maintain a yearly tenth-million revenue system :/ )

Tribasic answered 13/12, 2010 at 2:41 Comment(1)
Notice that all those typedef declarations are valid. None is invalid C++.Radom
E
17

The pointer symbol binds to the type by default, so the function pointer needs the parenthesis to indicate that the pointer is actually on the name and not on the return type.

Embezzle answered 13/12, 2010 at 2:43 Comment(0)
P
12

Without the parentheses, you're declaring a function returning a void*, not a pointer to a function returning void.

Procumbent answered 13/12, 2010 at 2:45 Comment(0)
B
4

The code below is accepted without a witter by GCC 4.2.1 on MacOS X 10.6.5 with the compiler set to fussy:

c++ -Wall -Wextra -c xx.cpp

Code:

typedef void *LPVOID;

typedef void    (*_tStandardDeclaration)(LPVOID);

typedef void    *_tDeclarationWithoutParenthesis(LPVOID);
typedef void*   _tAlternateDeclaration(LPVOID);

The first gives a pointer to a function returning void; the latter two are equivalent (spacing makes no difference) and give you a type that is 'function (taking LPVOID argument) that returns pointer to void'.

You can use them to declare function pointers:

typedef _tDeclarationWithoutParenthesis *_tFunctionPointer;

Fun, isn't it...

Brien answered 13/12, 2010 at 2:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.