简体   繁体   中英

persistent error: subscripted value is neither array nor pointer nor vector

I'm having problems with my college assignment and I would love some help.

The part of the code in question:

#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);
      }

In every instance of t[maxcounter] the following error appears:

 error: subscripted value is neither array nor pointer nor vector
         maxid = st[maxcounter].id;
                   ^

Do you know what's causing this? Have I not declared the struct right?

First of all,

  scanf("%s", name[MAXSTRING]);

is wrong, it should be

scanf("%99s", name);

That said, seeing the usage in the line

if (st[maxcounter].id > maxid){

is wrong, as st is defined as student st . st is not an array type, so you cannot use indexing on it, or in other words, you cannot use st as the operand of [] operator.

st is an argument to your function, it is a single value of type student . As such, you can't subscript it as the error says. t on the other hand is an array of student .

Looking at your code, where you have

st[maxcounter].id > maxid

you probably meant

t[maxcounter].id > maxid

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM