1

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 !

2
  • 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 Commented Mar 16, 2018 at 2:52
  • &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). Commented Mar 16, 2018 at 3:09

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.