20181445-2.1.c: In function ‘main’: 20181445-2.1.c:90:29: warning: passing argument 2 of ‘waitpid’ from incompatible pointer type [-Wincompatible-pointer-types] 90 | waitpid(pid,&stat,0); | ^~~~~ | | | int (*)(const char * restrict, struct stat * restrict) In file included from 20181445-2.1.c:7: /usr/include/x86_64-linux-gnu/sys/wait.h:111:45: note: expected ‘int *’ but argument is of type ‘int (*)(const char * restrict, struct stat * restrict)’ 111 | extern __pid_t waitpid (__pid_t __pid, int *__stat_loc, int __options);
时间: 2024-02-14 14:06:44 浏览: 210
这个编译警告是因为waitpid函数的第二个参数应该是一个指向int类型的指针,但是你传递的参数类型是一个指向函数的指针,因此参数类型不兼容。你需要修改代码,将第二个参数&stat改为指向int类型的指针变量,例如:
```
int status;
waitpid(pid, &status, 0);
```
这个修改会将status变量的地址传递给waitpid函数,使得waitpid函数能够在子进程退出时将子进程的状态信息存储到该变量中。这样就能解决编译警告的问题。
相关问题
demo1.c: In function ‘func1’: demo1.c:9:2: warning: return makes pointer from integer without a cast [enabled by default] demo1.c: In function ‘main’: demo1.c:17:2: error: ‘parm’ undeclared (first use in this function) demo1.c:17:2: note: each undeclared identifier is reported only once for each function it appears in demo1.c:17:2: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [enabled by default] /usr/include/pthread.h:225:12: note: expected ‘void * (*)(void *)’ but argument is of type ‘int *’
这些错误和警告是由于你的代码中存在一些问题:
1. 在 func1 函数中,你返回了一个指针类型,但是函数定义中的返回值类型是 void*,需要将返回值强制转换为 void* 类型。
2. 在 main 函数中,你使用了一个未声明的变量 parm,导致编译出错。
3. 在调用 pthread_create 函数时,第三个参数的类型应该是 void* (*)(void *),即指向函数的指针类型,但是你传递的是 int* 类型的指针,需要进行类型转换。
修改后的代码如下所示:
```
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *func1(void *arg)
{
int *p = (int *)arg;
printf("Thread %d is running.\n", *p);
pthread_exit(NULL);
}
int main()
{
pthread_t tid;
int i = 1, ret;
ret = pthread_create(&tid, NULL, func1, &i);
if (ret != 0) {
printf("Create thread error!\n");
exit(1);
}
pthread_join(tid, NULL);
printf("Thread %d exit.\n", tid);
return 0;
}
```
请注意,为了避免出现警告或错误,我们对代码进行了一些修改。
2.c: In function ‘main’: 2.c:32:3: warning: implicit declaration of function ‘strcpy’ [-Wimplicit-function-declaration] strcpy(a[i].name,name_a); ^~~~~~ 2.c:32:3: warning: incompatible implicit declaration of built-in function ‘strcpy’ 2.c:32:3: note: include ‘<string.h>’ or provide a declaration of ‘strcpy’ 2.c:33:10: warning: passing argument 1 of ‘strcpy’ makes pointer from integer without a cast [-Wint-conversion] strcpy(a[i].age,age_a); ^ 2.c:33:10: note: expected ‘char *’ but argument is of type ‘int’ 2.c:33:19: warning: passing argument 2 of ‘strcpy’ from incompatible pointer type [-Wincompatible-pointer-types] strcpy(a[i].age,age_a); ^~~~~ 2.c:33:19: note: expected ‘const char *’ but argument is of type ‘int *’ 2.c:34:10: warning: passing argument 1 of ‘strcpy’ makes pointer from integer without a cast [-Wint-conversion] strcpy(a[i].id,id_a); ^ 2.c:34:10: note: expected ‘char *’ but argument is of type ‘int’ 2.c:34:18: warning: passing argument 2 of ‘strcpy’ from incompatible pointer type [-Wincompatible-pointer-types] strcpy(a[i].id,id_a); ^~~~
这个警告信息是因为代码中使用了`strcpy()`函数,但是没有包含`<string.h>`头文件,所以编译器无法识别该函数的声明。同时,还存在一些参数不匹配的问题,例如传递给`strcpy()`函数的参数类型与函数原型不匹配等。
要解决这个问题,可以在代码中包含`<string.h>`头文件,这样编译器就可以识别`strcpy()`函数的声明。另外,需要注意传递给`strcpy()`函数的参数类型是否正确,例如第33行和第34行传递的参数类型应该是`char *`,而不是`int`类型。
修复代码如下所示:
```c
#include <stdio.h>
#include <string.h>
struct student{
char name[20];
char age[10];
char id[20];
};
int main(){
struct student a[100];
int i=0, n=0;
char name_a[20], age_a[10], id_a[20];
printf("Please input n: ");
scanf("%d", &n);
for(i=0;i<n;i++){
printf("Please input name, age, id: ");
scanf("%s %s %s", name_a, age_a, id_a);
strcpy(a[i].name,name_a);
strcpy(a[i].age,age_a);
strcpy(a[i].id,id_a);
}
for(i=0;i<n;i++){
printf("%s %s %s\n",a[i].name,a[i].age,a[i].id);
}
return 0;
}
```
阅读全文