所以当我尝试在Linux上编译名为pipe的代码时收到此错误

pipe.c: In function ‘main’:
pipe.c:27:14: error: ‘Amsg’ undeclared (first use in this function)
 write(fd[1], Amsg, strlen (Amsg));
              ^
pipe.c:27:14: note: each undeclared identifier is reported only once for each function it appears in
pipe.c:30:41: error: expected ‘;’ before ‘:’ token
 printf("A got: %s and i = %d\n", buf, i):}

这是我的代码:
#define SIZE 1000
#include <stdio.h>
#include <string.h>


int main(void) {

int fd[2];
char buf[SIZE];

char *AMsg = "nodeA";
char *Bmsg = "nodeB";
int i = SIZE;
pipe(fd);


if( fork() == 0) {
sleep(1);
read(fd[0], buf, 100);
i++;
printf("B got: %s and i = %d\n", buf, i);
write(fd[1], Bmsg, strlen(Bmsg));
// sleep(10);
}

else {
write(fd[1], Amsg, strlen (Amsg));
wait(NULL);
while((i = read(fd[0], buf, 100)) != 0) {
printf("A got: %s and i = %d\n", buf, i);}
}
}

我该如何解决?让我感到困惑的是sleep(1)是什么意思?这是否意味着1是正确的,如果它是1,它将进休眠眠过程?

最佳答案

似乎是一个简单的语法错误。
您声明了“char AMsg”
然后尝试将变量称为Amsg。
您只需将m更改为M。

关于linux - 在Linux上编译错误(有关管道概念的简单演示代码),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30835382/

10-13 05:27