Macro for function and function pointer declarations?

11,613

Solution 1

Omit the parentheses around Pro_Window*:

void PRO_SIGNAL(paint, Pro_Window*);

Macro parameters are substituted literally, so you ended up with:

void (*paint[PRO_SIGNAL_MAX])((Pro_Window*));

which is a syntax error.

Also, it is a good practice to enclose macro parameters in parentheses in the macro itself, since you never know whether the caller will pass an expression or a single token:

#define PRO_SIGNAL(func, param) (*(func)[PRO_SIGNAL_MAX])(param)

Solution 2

Its the (Pro_Window*) part. Your PRO_SIGNAL( paint, (Pro_Window*) ) will expand to

(*paint [ PRO_SIGNAL_MAX ])((Pro_Window*))

I suppose the compile is not happy about the nested parenthesis.

When baffled by macro expansion, it's often good to look at what the preprocessor actually fed the compiler. How to generate the preprocessed code varies with compilers, though.

Share:
11,613
ApprenticeHacker
Author by

ApprenticeHacker

I try to make the internet faster.

Updated on June 09, 2022

Comments

  • ApprenticeHacker
    ApprenticeHacker almost 2 years

    I'm trying to create a Macro function for defining function pointers , functions etc.

    Here's what I'm trying to do:

    #define PRO_SIGNAL_MAX 5
    #define PRO_SIGNAL( func, param ) (*func [ PRO_SIGNAL_MAX ])(param)
    

    I want to use this to declare an array of function pointers of size PRO_SIGNAL_MAX.

    So, when I use this here:

    void PRO_SIGNAL( paint, (Pro_Window*) ); 
    

    I want it to generate:

    void (*paint [ 5 ])(Pro_Window*) ;
    

    but it isn't working quite as I planned, I get this error:

    pro_window.c|16|error: expected declaration specifiers or '...' before '(' token|
    

    What exactly is the problem?