c-can';无法使用scanf()获取多个字符串

c-can';无法使用scanf()获取多个字符串,c,string,scanf,C,String,Scanf,我一直在努力用空格分隔两行,我的scanf()无法正常工作 这是密码 char *str1, *str2; while(1) { printf("Enter string:\n"); scanf(" %s", str1); printf("Then;"); scanf(" %s", str2); if(strcmp(str1, "exit") == 0){b

我一直在努力用空格分隔两行,我的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:
scanf中的“%s”
(“%s”,str1)要求str1指向有效内存以存储字符串<代码>str1未初始化-它未指向有效内存

而是提供有效内存并限制输入宽度

char str[100];
scanf("%99s", str1);
当传递到
scanf(“%99s”,str1)时,数组
str
转换为指向数组开头的指针

“%s”
中的前导空格不需要,因为
“%s”
本身占用前导空格,

就像

你从来没有初始化过
str1
str2
。把它们做成数组而不是指针<代码>字符str1[100],str2[100]好的malloc成功了谢谢你@Barmar