我的大学作业出了点问题,我需要一些帮助。
有问题的代码部分:
#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]实例中都会出现以下错误:
error: subscripted value is neither array nor pointer nor vector
maxid = st[maxcounter].id;
^你知道这是什么原因吗?我没有声明结构是正确的吗?
发布于 2018-05-31 16:55:53
首先,
scanf("%s", name[MAXSTRING]);是错的,那就应该是
scanf("%99s", name);这就是说,看看这行中的用法
if (st[maxcounter].id > maxid){是错误的,因为st被定义为student st。st不是数组类型,因此不能对其使用索引,换句话说,不能将st用作[]运算符的操作数。
发布于 2018-05-31 17:10:45
st是函数的参数,它是student类型的单个值。因此,您不能像错误所说的那样给它加上下标。另一方面,t是一个student数组。
看看你的代码,你在哪里
st[maxcounter].id > maxid你的意思可能是
t[maxcounter].id > maxidhttps://stackoverflow.com/questions/50620290
复制相似问题