value既不是数组也不是指针
我有这个代码:
int ID [STUDENTS]; int Scores [STUDENTS][GRADES]; int Hi [GRADES]; int Lo [GRADES]; double Avg [GRADES]; int numStudents; /*getData function called*/ int getData(ID, Scores, numStudents) { int student_count; int quiz_count; FILE* spIn; spIn = fopen("myfile.dat", "r"); student_count=0; while (fscanf (spIn, "%d", ID[student_count]) != EOF) { for (quiz_count = 0; quiz_count < GRADES; quiz_count++) { fscanf (spIn, "%d", Scores[student_count][quiz_count]); } } }
我一直得到行标题的错误: while(fscanf (spIn, "%d", ID[student_count]) !=EOF)
和fscanf (spIn, "%d", Scores[student_count][quiz_count]);
请帮忙我不知道该怎么办!
问题在于变量ID
和Scores
的范围。 两个变量都在两个地方定义 –
- 作为文件顶部的全局变量,和
- 作为您的函数的命名参数。
命名参数的范围更接近使用地点,因此它获胜。
它看起来像在第二位使用(即作为函数参数)不是故意的,因为声明缺少类型* 。 删除它们将使您的代码编译。
*在这种情况下,C认为它们是int
,并发出一个警告,该错误应该向您解释错误:由于编译器认为内部范围ID
和Score
是int
,因此您不能使用index operator []
。
fscanf()的两个实例都不正确。 fscanf()需要指针而不是整数来修改该位置的内存。
你的第一个电话是:
while (fscanf (spIn, "%d", ID[student_count]) != EOF)
索引变量ID
您将student_count
索引的值传递给函数。 哪个fscanf()试图将其解释为内存位置(不好)!
这个:
while (fscanf (spIn, "%d", &ID[student_count]) != EOF)
将索引student_count
的数组引用传递给fscanf()。
对fscanf()的另一个调用也是如此!