1
votes

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 !

1
You should show the declaration of create_button. Also, if that is a declaration in a structure, then you cannot just write on_click, you have to use a structure access operator with a structure variable - M.M
&on_click((void *)box) is wrong because on_click((void *)box) is not a function, it's a function call. It should be &on_click (or just on_click for short). - user253751

1 Answers

1
votes

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*)