C: PThread_create Parse Char [] for a function

Hallo all

I have this method:

void *readFileLocal(char filename[]){ ..... } 

Now I want to run this a thread method:

 char input[strlen(argv[1])]; strcpy(input,argv[1]); pthread_t read,write; pthread_create(&read, NULL, &readFileLocal, &input); 

But at compile time, it gives the following warning:

 file.c:29: warning: passing argument 3 of 'pthread_create' from incompatible pointer type 

/usr/include/pthread.h:227: note: expected 'void * (*) (void *), but the argument is of type' void * (*) (char *)

How can I parse the char array for my function by pthread_create without this warning? Thanks for helpt

+4
source share
2 answers

Just use this:

 pthread_create(&read, NULL, readFileLocal, input); 

And think about changing your function signature:

 void *readFileLocal(void *fileName) { } 

When you pass a pointer to a function (for example, the one you use in the readFileLocal parameter), you do not need to put & .

Also, when you have an array (e.g. input in your case), you don't need & , since C arrays can be used as pointers already.

+2
source

Functions for threads should be prototyped:

 void *func(void *argv); 

As with all void pointers, you need to interpret ("throw") a pointer to a significant entity. Then you read the FileLocal function:

 void *readFileLocal(void *argv) { char *fname = argv; // Cast to string // Rest of func } 
+2
source

All Articles