Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如何将C字符串数组传递给线程(pthreads)_Linux_Pthreads - Fatal编程技术网

Linux 如何将C字符串数组传递给线程(pthreads)

Linux 如何将C字符串数组传递给线程(pthreads),linux,pthreads,Linux,Pthreads,这就是我到目前为止所拥有的,当我编译它时,我得到以下错误: lb54.c:在函数“funct1”中: lb54.c:38:2:警告:格式“%s”要求参数类型为“char*”,但参数2的类型为“int”[-Wformat=] printf(“%s\n”,名称[i]); ^ 当我将%s更改为%d时,它会工作,但会显示一些随机数 #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include

这就是我到目前为止所拥有的,当我编译它时,我得到以下错误: lb54.c:在函数“funct1”中: lb54.c:38:2:警告:格式“%s”要求参数类型为“char*”,但参数2的类型为“int”[-Wformat=] printf(“%s\n”,名称[i]); ^

当我将%s更改为%d时,它会工作,但会显示一些随机数

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>

void * funct1(void* arg);
void * funct2(void* arg);

void main(){

char name[10][20];
int *id = (int*)malloc(sizeof(int)*10);

int i,x=5;

for(i=0;i<10;i++){
strcpy(name[i],"name");
id[i] = i;
}

pthread_t threadid;
pthread_t threadname;


pthread_create(&threadid,NULL,funct2,(void *)id);
pthread_create(&threadname,NULL,funct1, &name);
sleep(5);
free(id);
printf("parent thread exiting\n");

}

void * funct1(void* arg){
int i;
char *name = (char *)arg;
for(i=0;i<10;i++){
 printf("%s\n",name[i]);
}
}

void * funct2(void* arg){
int i;
int *id = (int *) arg;
for(i=0;i<10;i++){
 printf("%d\n",id[i]);
}
}
#包括
#包括
#包括
#包括
void*funct1(void*arg);
void*funct2(void*arg);
void main(){
字符名[10][20];
int*id=(int*)malloc(sizeof(int)*10);
int i,x=5;

对于(i=0;i您已在
funct1
中声明变量
name
的类型为char*。当您使用[i]访问它时,它将仅成为char,因此不适用于%s


如果您确定名称变量是字符串数组,请将其声明为
char**name
char*name[]

您已在
funct1
中声明变量
name
为char*类型。当您使用[i]访问它时,它将变为仅char,因此不适用于%s


如果您确定name变量是一个字符串数组,请将其声明为
char**name
char*name[]
谢谢您的回答,我通过在for循环中为每个名称条目使用malloc(name[I]=(char*)malloc(20);)谢谢您的回答,我通过在for循环中为每个名称条目使用malloc(name[I]=(char*)malloc(20);)