C: PThread_create 解析 Char[] 参数到函数
大家好,
我有这个方法:
void *readFileLocal(char filename[]){
.....
}
现在我想在线程中启动这个方法:
char input[strlen(argv[1])];
strcpy(input,argv[1]);
pthread_t read,write;
pthread_create(&read, NULL, &readFileLocal, &input);
但是在编译过程中它给出了这个警告:
file.c:29: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type
/usr/include/pthread.h:227: note: Expected 'void * (*)(void *) ' 但参数的类型为 'void * (*)(char *)'
如何在没有此警告的情况下通过 pthread_create 将 char 数组解析为我的函数? 谢谢你的帮助
Hallo All,
I have this method:
void *readFileLocal(char filename[]){
.....
}
Now i want to start this method 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需使用这个:
并考虑将函数的签名更改为:
当您传递指向函数的指针时(例如您在
readFileLocal
参数中使用的指针),您不需要将&
。另外,当您有一个数组(例如您的情况下的
input
)时,您不需要&
因为在 C 中数组可以用作已经指点了。Just use this:
And consider changing your function's signature to:
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.线程的函数需要原型化:
与所有 void 指针一样,您需要将指针解释(“转换”)为有意义的实体。你读FileLocal函数然后变成:
Functions for threads need to be prototyped:
As with all void pointers you then need to interpret ("cast") the pointer to a meaningful entity. You readFileLocal functions then becomes:
它只需要看起来像这样。
不要忘记强制类型转换。
It just needs to look like this.
Don't forget to force conversion of types.