C: Type coercion of function pointers
Posted: Sat Jun 01, 2024 9:38 am
I'm working on decompiling a program to C (I don't know for sure, but so far it appears that it had originally been written in C). It has a callback system where a function can be scheduled to be run later. These are stored in an array of structs with information on when and how to call the function, and a pointer to that function. These functions always return a pointer of some kind, and always take one argument which is a pointer. However, the type of that pointer depends on the particular function.
So I thought the obvious solution was that the type in the struct is, and then I can create a function of the type, for example, because void pointers can be freely coerced back and forth between particular pointer types.
However, gcc is giving me an incompatible pointer warning for this behaviour. (it does work the way I expected, but I'd rather do it right).
What is the best way to deal with function pointers that can take in pointers of different types? Should I just be explicitly casting it? Is there a version of the C standard that does allow this coercion? (I haven't specified any particular standard or dialect on the command line) Does C allow any automatic type coercion between different function pointers?
So I thought the obvious solution was that the type in the struct is
Code: Select all
void * (*)(void *)
Code: Select all
int * (*)(int *)
However, gcc is giving me an incompatible pointer warning for this behaviour. (it does work the way I expected, but I'd rather do it right).
What is the best way to deal with function pointers that can take in pointers of different types? Should I just be explicitly casting it? Is there a version of the C standard that does allow this coercion? (I haven't specified any particular standard or dialect on the command line) Does C allow any automatic type coercion between different function pointers?