I keep getting an error when trying to compile my code. The error is as follows :
warning: incompatible pointer types passing
'void *(threadData *)' to parameter of type 'void * (*)(void *)'
[-Wincompatible-pointer-types]
pthread_create(&threads[id], NULL, start,&data[id]);
I'm trying to pass a struct to the function, void * start(threadData* data)
, and this keeps throwing me off. Any ideas?
It's complaining about the thread function (bound to the third parameter of
pthread_create
), you can modify that to take avoid *
argument and then cast it back before doing anything with it:You may also opt to coerce the data pointer (fourth parameter) to the expected type:
but I don't think that's necessary since a
void *
is supposed to be freely convertible to and from most other pointers.You can see the problem in this small yet complete program:
When compiling that, you see:
Keep in mind that it's just a warning, not an error but, if you want your code to compile cleanly, it's worth getting rid of. Making the changes mentioned at the top of this answer (bar the data parameter casting) gives you the following thread function:
This compiles without warnings, and runs just fine.