我不知道如何访问 args 中包含的字符指针。 args 是传递给 pthread_create 的字符点数组。我应该将参数存储在结构体而不是数组中吗?
int loop(void* args){
struct dirent *direntp; DIR *start_dir;struct stat statbuf;char *dirs[MAX_SIZE_DIRS];int error; const char* sd;
int size = 0;
char* fn = (char *)(args); sd = (char *)(args + 1); char* path = (char *)(args+2);
printf("%s\n", (char *)args);
最佳答案
也许你想要:
int loop(void* args){
char** arr = args;
char* fn = arr[0];
char* sd = arr[1];
char* path = arr[2];
(事实上,出于可读性的原因,最好在线程函数之外声明一些struct
并使用它的地址,因为不同的元素具有不同的角色)
在尝试编写多线程程序之前,您应该花几天时间阅读一本好的 C 编程书籍(例如使用 POSIX threads )。
顺便说一句,函数传递给 pthread_create(3)应该返回一个通用指针(即一些 void*
),而不是 int
;所以你的loop
有错误的签名传递给pthread_create
。另请阅读 intptr_t
关于c - 如何从传递给线程函数的参数数组中获取字符指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34305337/