Lucas
Lucas

Reputation: 33

Passing a function pointer with void pointer argument to a function

I have a function pointer in a structure, with a void* argument. I would like to pass this function to another one, but I get compilation errors...

This is the function declaration in the structure:

void (*on_click)(void *);

And this is how I pass the function pointer to my function, where box is a structure:

but1 = create_button(posbut, "test", BUTTON_DEF, &on_click((void *)box));

I have the error: cannot convert to a pointer type, and lvalue required as unary ‘&’ operand on this line.

Thanks !

Upvotes: 1

Views: 1432

Answers (1)

Pablo
Pablo

Reputation: 13590

You would have to pass the function pointer like this1:

but1 = create_button(posbut, "test", BUTTON_DEF, on_click);

I don't know which framework you are using, so I don't know if there are other arguments that you have to pass to create_button, but most propbably create_button will be the one that calls the function pointer with the correct argument.


fotenotes

provided that the last argument of create_button is a void (*callback)(void*)

Upvotes: 1

Related Questions