尝试使用以下代码将输入存储到字符串数组中:
#include <stdio.h>
#include <string.h>
int main()
{
int noOfStrings;
printf("Enter no of strings: ");
scanf("%d", &noOfStrings);
char *string[noOfStrings];
for(int i=0; i<noOfStrings; i++){
printf("\nEnter string %d: ", i);
scanf("%s\n",string[i]);
}
return 0;
}
-----------------------------------------------------------------------------
Console:
Enter no of strings: 3
Enter string 0: abc
Enter string 1: def
Enter string 2: ghi
Segmentation fault (core dumped)
...Program finished with exit code 139
Press ENTER to exit console.
-----------------------------------------------------------------------------
我无法弄清楚为什么会失败。
我还尝试了以下具有固定大小数组的代码。
#include <stdio.h>
#include <string.h>
int main()
{
int noOfStrings;
printf("Enter no of strings: ");
scanf("%d", &noOfStrings);
char string[noOfStrings][5];
for(int i=0; i<noOfStrings; i++){
printf("\nEnter string %d: ", i);
scanf("%s\n",string[i]);
}
printf("\nPrinting Stored Strings");
for(int i=0; i<noOfStrings; i++){
printf("\nEnter string %d: ", i);
printf("%s\n",string[i]);
}
return 0;
}
-----------------------------------------------------------------------------
Console:
Enter no of strings: 3
Enter string 0: abc
def
Enter string 1: ghi
Enter string 2: jkl
Printing Stored Strings
Enter string 0: abc
Enter string 1: def
Enter string 2: ghi
...Program finished with exit code 0
Press ENTER to exit console.
输入第一个字符串('abc')后,没有提示输入第二个字符串,因此请输入'def'。其次是2个字符串。请注意,未打印字符串“ jkl”。
请告诉我这两个案件中我缺少什么?
谢谢。
答案 0 :(得分:2)
在第一种情况下,您要定义一个指针数组:
digraph G {
rankdir=RL;
graph [fontsize=10 fontname="Verdana"];
node [style=filled height=0.55 fontname="Verdana" fontsize=10];
subgraph cluster_key {
label="Key";
progress [fillcolor="wheat" label="In progress"];
todo [label="To do"];
done [fillcolor=palegreen3 label="Done"];
not_our [fillcolor=none label="Not our\nteam"];
numbers [color=none label="Numbers\nrepresent\nperson\ndays"];
progress -> done [style=invis];
todo -> progress [style=invis];
not_our -> todo [style=invis];
numbers -> not_our [style=invis];
}
mappings [fillcolor=palegreen3];
identifiers [fillcolor=palegreen3];
hyperwarp [fillcolor=wheat];
ghost [fillcolor=none]
UI [fillcolor=none]
events [fillcolor=wheat];
flag [fillcolor=palegreen3];
groups [fillcolor=wheat];
types [fillcolor=wheat];
instances [];
resources [];
optimize [];
remove_flag [];
persist [];
approval [];
edge [style="" dir=forward fontname="Verdana" fontsize=10];
types -> flag;
groups -> events;
events -> {flag mappings identifiers};
ghost -> hyperwarp;
UI -> ghost;
resources -> identifiers;
optimize -> groups;
hyperwarp -> flag;
instances -> {ghost UI types events hyperwarp flag};
resources -> {groups flag};
remove_flag -> approval;
persist -> approval;
approval -> {types resources instances};
}
但是,这些指针未初始化。然后,当您尝试使用char *string[noOfStrings];
时,将取消引用这些无效的指针。这样做会调用undefined behavior,在这种情况下会导致崩溃。
第二种情况是通过使用2D字符数组来解决此问题的,该字符数组应足以容纳您输入的字符串。但是您会因为scanf
格式而陷入困境:
scanf
格式字符串中的scanf("%s\n",string[i]);
匹配任意数量的空格字符,因此该函数只有在您输入非空白字符后才会返回。您可以通过从格式字符串中删除\n
来解决此问题。
\n