Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function Pointer Declaration - what does __P do?

The usual form of function pointer definitions is:

 int function(int, int);
 int (*ptr)(int, int);

but I saw a form today which I didn't understand. Can anyone explain this please?

int (*close)    __P((struct __db *));
like image 342
Cruisehu Avatar asked Apr 20 '13 16:04

Cruisehu


3 Answers

The __P() macro is usually used to support C implementations from the days of K&R C, when there were no prototypes (which were introduced to C with C89). Basically the logic is

#if SOME_LOGIC_TO_TEST_WHETHER_IMPLEMENTATION_SUPPORTS_PROTOTYPES
#  define __P(argument_list) argument_list
#else
#  define __P(argument_list) () 
#endif

Can you see how this works when applied to your example? Note that for this to work and not cause a syntax error, the argument list must include the parentheses of the function call, not just the parentheses of the function-like macro. Hence the double parentheses when the macro is used. That's probably the reason why it looks unusual.

like image 112
Jens Avatar answered Sep 22 '22 01:09

Jens


__P() is just a macro. On my system it is defined as follows (in sys/cdefs.h):

#if defined(__STDC__) || defined(__cplusplus)
#define __P(protos)     protos          /* full-blown ANSI C */
#else   /* !(__STDC__ || __cplusplus) */
#define __P(protos)     ()              /* traditional C preprocessor */
#endif  /* !__GNUC__ */

From this, it seems to be used to maintain compatibility with (very) old compilers.

like image 41
NPE Avatar answered Sep 26 '22 01:09

NPE


The usual form of function pointer definitions is .... but I saw a form today which I didn't understand.

There is nothing special here, no magic syntax. This is not a different form of function pointer declaration.

This is just the standard form of function pointer declaration, and __P() is a macro defined by one of the header files that you are using. So, find that macro definition to learn what its purpose is.

like image 37
David Heffernan Avatar answered Sep 25 '22 01:09

David Heffernan