Search code examples
cfunctiondeclarationfunction-prototypes

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 *));

Solution

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