Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

expected ')' before '*' token with function pointer

here is the code:

typedef struct {
    void (*drawFunc) ( void* );
} glesContext;

void glesRegisterDrawFunction(glesContext *glesContext, void(drawFunc*)(glesContext*));

For that last line, i get the error message: "Expected ')' before '*' token"

why?

like image 987
Cliff Porter Avatar asked Feb 20 '12 02:02

Cliff Porter


2 Answers

You have the correct way of doing a function pointer in your struct (so kudos for that, so many people get it wrong).

Yet you've swapped around the drawFunc and * in your function definition, which is one reason why the compiler is complaining. The other reason is that you have the same identifier being used as the type and the variable. You should choose different identifiers for the two different things.

Use this instead:

void glesRegisterDrawFunction(glesContext *cntxt, void(*drawFunc)(glesContext*));
                                                       ^^^^^^^^^
                                                       note here
like image 169
paxdiablo Avatar answered Sep 19 '22 04:09

paxdiablo


One solution is to add a pointer to function typedef as follows:

typedef struct {
    void (*drawFunc) ( void* );
} glesContext;

// define a pointer to function typedef
typedef void (*DRAW_FUNC)(glesContext*);

// now use this typedef to create the function declaration
void glesRegisterDrawFunction(glesContext *glesContext, DRAW_FUNC func);
like image 20
mrsheen Avatar answered Sep 19 '22 04:09

mrsheen