Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why parentheses are important in function pointer declaration?

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 :/ )

like image 755
YeenFei Avatar asked Dec 13 '10 02:12

YeenFei


3 Answers

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.

like image 129
MikeP Avatar answered Nov 15 '22 08:11

MikeP


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

like image 32
dan04 Avatar answered Nov 15 '22 10:11

dan04


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...

like image 4
Jonathan Leffler Avatar answered Nov 15 '22 10:11

Jonathan Leffler