The declaration typedef int (*print_int_func) (int);
declares print_int_func
to be a pointer to a specific type of function. So (print_int_func)p[0]
casts p[0]
to such a pointer to a function, but (print_int_func*)p[0]
casts p[0]
to a pointer to a pointer to a function. Thus, the result is a pointer to an object (that object being a pointer to a function). Since it is an object, not a function (or pointer to a function), it cannot be called like a function.
Additionally, avoid using void *
for pointers to functions. void *
is a pointer to an object, and the C standard does not define conversions between pointers to objects and pointers to functions. To create a “generic” pointer to a function, simply choose any function type and use a pointer to that type:
- Convert to the chosen type when storing the pointer.
- Convert to the actual function type when calling the function type.
For example, you can declare an arbitrary type:
typedef void (*CommonFunctionPointer)(void);
and make an array of them:
CommonFunctionPointer *p = malloc(N * sizeof *p);
,
and then you can store any function pointer in the array:
p[i] = (CommonFunctionPointer) print_int;
and use a pointer from the array by casting it back to its correct type:
((int (*)(int)) p[i])(2);
.