سؤال

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 *));
هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top