Strictly speaking, this is not possible. According to the standard, a pointer to void can simply be converted to a pointer or from a pointer to an object type. On some architectures, function addresses are greater than object addresses.
C11, ยง 6.3.2.3 Pointers
A pointer to void can be converted to a pointer to or from a pointer to any type object. A pointer to any type of object can be converted to a pointer to void and vice versa; The result is compared with the original pointer.
Otherwise, this is a general extension.
C11, ยง J.5.7. Function Index
A pointer to an object or void can be transferred to a pointer to a function that allows you to call data as a function (6.5.4).
In your example, you are not calling func .
#include <stdio.h> #include <pthread.h> #include <unistd.h> pthread_t idThread; void aFunction(void) { while (1) { fprintf(stderr, "I've been called!\n"); usleep(1000000); } } void *threadFunction(void *argFunc) { void (*func)(void) = argFunc; func(); /* HERE */ } int thread_creator(void (*f)(void)) { pthread_create(&idThread, NULL, threadFUnction, (void *) f); } int main(void) { thread_creator(aFunction); while (1); return 0; }
source share