【发布时间】:2021-02-27 06:31:57
【问题描述】:
我一直在努力将两行用空格隔开,而我的 scanf() 无法正常工作 这是代码
char *str1, *str2;
while(1) {
printf("Enter string:\n");
scanf(" %s", str1);
printf("Then;");
scanf(" %s", str2);
if(strcmp(str1, "exit") == 0){break;}
printf("Output:\n %s %s\n", str1, str2);
}
但我的输出看起来像这样:
Enter string:
ok
Then;hello
Output:
ok (null)
Enter string:
Then;
什么会导致这个问题?它会在循环第二次转动时打印第一个输出。
Enter string:
ok
Then;hello
Output:
ok (null)
Enter string:
Then;well
Output:
hello (null)
Enter string:
Then;no
Output:
well (null)
Enter string:
【问题讨论】:
-
你从未初始化过
str1和str2。 -
使它们成为数组而不是指针。
char str1[100], str2[100]; -
好的,malloc 工作了,谢谢@Barmar