qua*_*sar 0 c arrays pthreads void-pointers
我正在尝试创建一个采用字符串形式的线程argv[1]并将其传递给我的函数.这是我试图传递它的代码.
if(pthread_create(&thread1, NULL, getMax, &argv[1]) != 0){
printf("ERROR createing the thread\n");
return 1;
}
Run Code Online (Sandbox Code Playgroud)
这是我在pthread_create函数中调用的函数.
void * getMax(void * f){
char * fileName = (char*)f;
printf("%s\n\n",fileName);
}
Run Code Online (Sandbox Code Playgroud)
我相信我的问题是当我将它转换回字符指针时.该printf函数打印出几个随机字符.如果我调用函数传递一个字符串就行了.
pthread_create(&thread1, NULL, getMax, "This Works");
Run Code Online (Sandbox Code Playgroud)
如果有人可以解释如何施放argv[1]它,使其行为像一个非常感激的字符数组.
请记住,这&argv[1]是一个char*[]指向字符的指针数组.将它转换为char *ie指向字符的指针是不正确的.
尝试:
pthread_create(&thread1, NULL, getMax, argv[1]);
Run Code Online (Sandbox Code Playgroud)