Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Macro for function and function pointer declarations?

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?

like image 781
ApprenticeHacker Avatar asked Jun 09 '26 05:06

ApprenticeHacker


1 Answers

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)
like image 79
Blagovest Buyukliev Avatar answered Jun 10 '26 19:06

Blagovest Buyukliev