【发布时间】:2018-06-07 05:15:39
【问题描述】:
用户传入输入,该输入存储在 argv[2] 中。我将此值存储到缓冲区中,然后尝试通过我的 pthread_create 函数将该值传递给另一个函数。但是,这会扭曲缓冲区的值。
我传入输入,可能是“Hello”之类的内容,然后当它在“printFiles”函数中打印时,它是乱码。我该如何解决这个问题?
void *printFiles(void *file);
int main(int argc, char **argv)
{
pthread_t thread;
char *store = argv[2];
char *buffer = (malloc(500));
strcpy(buffer, store);
pthread_create(&thread, NULL, printFiles, (void *)&buffer);
pthread_join(&thread, NULL);
return 0;
}
void *printFiles(void *file)
{
printf("%s\n", file);
}
【问题讨论】: