[英]persistent error: subscripted value is neither array nor pointer nor vector
我在做大学作业时遇到问题,我希望得到一些帮助。
有问题的代码部分:
#include <stdio.h>
#include <stdlib.h>
#define MAXSTRING 100
int counter = 0;
int maxcounter = 0;
int maxid = 0;
typedef struct{
char name[MAXSTRING];
int id;
}student;
int AddStudent(student st, student *stArray) {
student t[] = {"",0};
int id;
char name[MAXSTRING];
printf("First enter the student's id\n");
scanf("%d", &id);
printf("Now enter the student's name\n");
scanf("%s", name[MAXSTRING]);
if (st[maxcounter].id > maxid){
maxid = t[maxcounter].id;
}
maxcounter++;
t[maxcounter].id = id;
t[maxcounter].name = name;
printf("%d", t[maxcounter].id);
}
在t[maxcounter]
的每个实例中, t[maxcounter]
出现以下错误:
error: subscripted value is neither array nor pointer nor vector
maxid = st[maxcounter].id;
^
你知道是什么原因造成的吗? 我没有声明结构正确吗?
首先,
scanf("%s", name[MAXSTRING]);
是错的,应该是
scanf("%99s", name);
也就是说,看到该行的用法
if (st[maxcounter].id > maxid){
是错误的,因为st
被定义为student st
。 st
不是数组类型,因此不能在其上使用索引,换句话说,不能将st
用作[]
运算符的操作数。
st
是函数的参数,它是student
类型的单个值。 因此,您不能按照错误说明对它进行下标。 另一方面t
是一群student
。
查看您的代码,在哪里
st[maxcounter].id > maxid
你可能是说
t[maxcounter].id > maxid
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.