【发布时间】:2016-09-07 08:57:35
【问题描述】:
我无法使用 scanf 在 stdnames 数组中输入名称。 编译时它没有错误,但是只要我输入名称和 然后按回车键写入另一个名称,它会出错并关闭程序。 我应该怎么做?
int main(int argc, char* argv[])
{
float marks[50];
/*char *stdnames[100]={"Arvind Thillainathan","Robert Lang"};*/
//I want to stores names like the above one
char *stdnames[100];
int totalNames = 0;
int i = 0, w=0,h=0;
printf("How many names do you want to enter ??\n");
scanf("%d",&totalNames);
assert(totalNames != 0);
for(int count = 0; count < totalNames; count++)
{
printf("Enter name of student\n");
scanf("%s",stdnames[count]);
//From here the problem starts
}
getres(marks,totalNames);
for(i = 0; i < totalNames; i++)
{
int v = 1;
printf("\n");
printf("IELTS Marks of %s\n\n",stdnames[i]);
for(h = w; h < w+5; h++)
{
if(v==1)
{
printf("Listening : %0.1f\n", marks[h]);
}
else if(v==2)
{
printf("Reading : %0.1f\n", marks[h]);
}
else if(v==3)
{
printf("Writing : %0.1f\n", marks[h]);
}
else if(v==4)
{
printf("Speaking : %0.1f\n", marks[h]);
}
else
{
printf("Overall : %0.1f\n\n", marks[h]);
}
v++;
//if(h==10)
//{
// break;
//}
}
w+=5;
}
return 0;
}
【问题讨论】:
-
您正在扫描到一个未分配的对象
stdnames[count]。也许先尝试在那里分配一些内存,比如malloc。 -
当你有
char *stdnames[100];时,你也可能会遇到float marks[50];的麻烦,尽管输入标记的循环有点模糊。 -
没有“字符数组”。你的意思是指向
char的指针数组? -
@arvind-dexter-thillainathan :考虑在代码中包含
getres(marks,totalNames)的定义。
标签: c arrays for-loop char scanf