I have the following code:
#include <stdlib.h>
#include <stdio.h>
typedef void (*func_t)(void * data);
void func2(int * arg, func_t free_func) {
free_func(arg);
}
void func(int * a) {
printf("%d\n", *a);
}
int main(int argc, char ** argv) {
int a = 4;
func2(&a, func);
return 0;
}
Compiling which gives me
warning: passing arg 2 of `func2′ from incompatible pointer type
Why is that? Shouldn’t int pointer be compatible with void pointer?
You can also do this:
Note though that converting a void * to T* (in this case int*)
is unsafe due to the side-effects of using a T* that is not actually pointing to a T
e.g.