我有一個計劃:在字符串和管道奇怪的行爲
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);
}
當我comiple這個文件,我總是得到警告:
[[email protected] 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);
結果:
[[email protected] temp]$ ./temp
hellon
用這一行,我得到了預期的結果:
[[email protected] temp$ ./temp
n is: 5
hello
爲什麼這條線太重要了? 謝謝!