[求助]那位大虾帮我瞧瞧,我看了半天看不懂
#include <stdio.h> #include <stdlib.h>
main() { char *str; char *p; str = (char *)malloc(100); p=str; while( *str++ = getchar() != '\n' ); *(str-1) = '\0'; printf("%s",p); getchar(); } 输入how are you!回车 为什么输出的却不是how are you!
[此贴子已经被作者于2005-10-19 22:42:47编辑过]
----------------解决方案--------------------------------------------------------
*str++ = getchar() != '\n' 中!=的优先级比=高,也就是说*str++ = getchar() != '\n'等价于 *str++ = (getchar() != '\n')
----------------解决方案--------------------------------------------------------
二楼说的很对.这样该一下就可以了: #include <stdio.h> #include <stdlib.h>
main() { char *str; char *p; str = (char *)malloc(100); p=str; while( (*str++ = getchar()) != '\n' ); *(str-1) = '\0'; printf("%s",p); getchar(); }
----------------解决方案--------------------------------------------------------