I couldn't quite find a definitive answer to this one. My function pointer points to two functions that both take a pointer as an argument. Pointers in the same program are all the same size. Is the following still undefined behavior? The key part here is that the pointer argument in the function pointer signature points to an object with a different type.
#include <stdio.h>
struct Test
{
int value;
};
int func1(void *userData)
{
struct Test *test = userData;
printf("func1: %i\n", test->value);
return 0;
}
int func2(struct Test *userData)
{
printf("func2: %i\n", userData->value);
return 0;
}
int main()
{
int (*func)(void *) = func1;
struct Test test = {0};
test.value = 9;
func(&test);
func = (int (*)(void *))func2;
func(&test);
return 0;
}
It compiles and runs fine on all compilers I have tried. Func1 is certainly correct (albeit not type safe because dealing with void*). However I am not sure about Func2 when being used / casted in this way.
I am basically looking at writing a callback system and want to avoid dealing with void* for safety and avoid code generation for maintenance.
Thanks!