I need help in function pointers.
I have two function pointer types:
typedef void (*draw_func1_t)(void* data, void* painter, double x, double y);
typedef void (*draw_func2_t)(void* data, MyPainter* painter, double x, double y);
The two types are almost the same, except the second parameter. Now I need to write a function that convert a draw_func1_t to draw_func2_t:
draw_func2_t convert_func_p(draw_func1_t func) { ... }
How can I write it? Can I just force a cast like
return (draw_func2_t)func;
because the two function prototypes are binary compatible?
If you cast a function pointer to a different type, then the behaviour on its calling is undefined. See Annex J.2 of the C standard:
Compatibility is dealt with in 6.7.5.1, paragraph 2:
A
MyPainter*is not compatible with avoid*. So your function pointer cast cannot be used to call the function.