开发者

C: PThread_create Parsing Char[] parameter to function

Hallo All,

I have this method:

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

Now i want to start this met开发者_C百科hod a a thread:

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

But during compilation it gives this 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 argument is of type ‘void * (*)(char *)’

How can I parse an char array to my funcation over pthread_create without this warning ? Thanks for helpt


Just use this:

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

And consider changing your function's signature to:

void *readFileLocal(void *fileName) { }

When you are passing a pointer to function (like the one you are using in readFileLocal parameter) you don't need to put &.

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


Functions for threads need to be prototyped:

void *func(void *argv);

As with all void pointers you then need to interpret ("cast") the pointer to a meaningful entity. You readFileLocal functions then becomes:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜