C 持续错误:下标值既不是数组,也不是指针或向量
我的大学作业有问题,我想得到一些帮助 有关守则的部分:C 持续错误:下标值既不是数组,也不是指针或向量,c,pointers,struct,declaration,C,Pointers,Struct,Declaration,我的大学作业有问题,我想得到一些帮助 有关守则的部分: #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, stu
#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);
}
你知道这是什么原因吗?我没有声明结构正确吗?首先
scanf("%s", name[MAXSTRING]);
这是错误的,应该是错误的
scanf("%99s", name);
这就是说,看看行中的用法
if (st[maxcounter].id > maxid){
错误,因为
st
定义为student st
st
不是数组类型,因此不能对其使用索引,或者换句话说,不能将st
用作[]
运算符的操作数。st
是函数的参数,它是student
类型的单个值。因此,您不能按照错误所述为其下标<另一方面,code>t是student
的数组
看看你的代码,你有
st[maxcounter].id > maxid
你可能是说
t[maxcounter].id > maxid
错误显示为
st
,而不是t
。