我有一个程序:
int main()
{
int* p_fd = (int*)malloc(2*sizeof(int));
char buf[100];
pipe(p_fd);
write(p_fd[1],"hello", strlen("hello"));
int n;
n = read(p_fd[0],buf,100);
//printf("n is: %d\n",n); // this line is important!
buf[n]="\0"; // this line triggers warning。
printf("%s\n",buf);
}
当我编译这个文件时,我总是收到警告:
[esolve@kitty temp]$ gcc -o temp temp.c
temp.c: In function ‘main’:
temp.c:38:9: warning: assignment makes integer from pointer without a cast [enabled by default]
没有这条线printf("n is: %d\n",n);
,结果是:
[esolve@kitty temp]$ ./temp
hellon
有了这条线,我得到了预期的结果:
[esolve@kitty temp$ ./temp
n is: 5
hello
为什么线路如此重要?谢谢!