I'm trying to use function pointer in my code, the code is as below.
#include <stdio.h>
#include <pthread.h>
typedef void (*PFUNC)(int);
typedef struct tag_FUNC_INFO_S
{
PFUNC callback;
int index;
} FUNC_INFO_S;
PFUNC callback_print(int index)
{
printf("[callback] index = %d\n", index);
return NULL;
}
void thread_test(FUNC_INFO_S *info)
{
info->callback(info->index);
pthread_exit(NULL);
}
int main()
{
pthread_t tid;
FUNC_INFO_S info;
info.callback = callback_print;
info.index = 777;
pthread_create(&tid, NULL, (void *)thread_test, &info);
printf("main printing\n");
return 0;
}
After compiled the code with "gcc -Wall xxx.c -o xxx -lpthread", the compiler complains with the following message:
func_ptr_test.c:30:16: warning: incompatible pointer types assigning to 'PFUNC' (aka 'void ()(int)') from 'PFUNC (int)' (aka 'void ((int))(int)' [-Wincompatible-pointer-types] info.callback = callback_print;
But, if I change the code from
info.callback = callback_print;
to
info.callback = (PFUNC)callback_print;
No warning message comes out any more. But, even without type casting, I think the "info.callback" has totally the same type with "callback_print", so I'm wondering why the warning message comes out. Does anyone have idea about this?
PFUNC callback_print(int index)-->void callback_print(int index)void * functionName( void *)Your thread functions do not have that syntaxmain()function the statement(s):int *status = NULL; pthread_join( tid, &status );is missingthread_test()should be:void *thread_test( void *param) { FUNC_INFO_S info = *( FUNC_INFO*)param;gcc -Wall xxx.c -o xxx -lpthreadwithgcc -Wall -Wextra -Wconversion -pedantic -std=gnu11 xxx.c -o xxx -lpthread