I was wondering if we can create a generic function pointer, which would point to a function with any return type in C. I tried it through this code:
typedef void (*func_ptr)(int, int);
int func1(int a, int b) {
return a * b;
}
int func2(int a, int b) {
return a + b;
}
int main() {
func_ptr ptr1, ptr2;
ptr1 = func1;
ptr2 = func2;
printf("Func1: %d\n", *(int *)(*ptr1)(4, 5));
printf("Func2: %d\n", *(int *)(*ptr2)(4, 5));
return 0;
}
If the func_ptr was of type int, the code would work. But what is wrong with this approach? Why can't we assign void * pointer to integer functions?