简体   繁体   English

下标值既不是数组也不是指针,也不是数组索引处的向量

[英]Subscripted value is neither array nor pointer nor vector at array index

(Homework question) I'm just learning C, and I'm making a program that reads data from a file, creates routers of that data, and puts pointers to the routers in an array of size 255, but I keep getting the title error on the line where I'm trying to add them to the array (作业问题)我只是在学习C,并且正在编写一个程序,该程序从文件中读取数据,创建该数据的路由器,并将指向路由器的指针放入大小为255的数组中,但是我一直在获得标题我试图将它们添加到数组的行上的错误

#define ARRAY_SIZE 255
struct router routers[ARRAY_SIZE] = {0};

int main(int argc, char *argv[]){
  unsigned char id;
  char name[32];
  struct router *new_router;

  if(argc == 2){
    //reads file with fread
    //setting id and name which prints out as expected

    new_router = make_router(id, name); //initialising method that returns a router pointer
    routers[new_router->id] = new_router; 
    //error occurs here, at [new_router->id]. Have also tried just using id
    }
 }

I've searched a lot of threads with the same error message, but they're all either someone who didn't declare an array, or were suggested to try it with unsigned char as index number, which is what I'm already using. 我搜索了很多具有相同错误消息的线程,但是它们要么都是未声明数组的人,要么被建议尝试使用无符号字符作为索引号进行尝试,这就是我已经在使用的东西。 Would love some insight into this. 希望对此有所了解。

struct router{
  unsigned char id;
  char name[32];
}

struct router* make_router(unsigned char id, char* name){
  struct router *r = malloc(sizeof(struct router));

  r->id = id;
  r->name = name;

  return r;
}

Assuming make_router allocates a struct dynamically, then 假设make_router动态分配一个结构,然后

routers[new_router->id] = *new_router; // note *

solves the compiler error. 解决了编译器错误。

However, you cannot copy structs like this if they have pointer members . 但是,如果它们具有指针成员,则不能复制这样的结构 You say that "Router is just a basic struct with an unsigned char for id, and a char* for name" so this is the case. 您说“ Router只是一个基本结构,其id为unsigned char,名称为char *”,因此就是这种情况。 But with an assignment like this, you won't get a hard copy of the pointed-at data. 但是通过这样的分配,您将不会获得指向数据的硬拷贝。

Pointers are not data. 指针不是数据。 They do not contain data. 它们不包含数据。 They point at data allocated elsewhere. 他们指向其他地方分配的数据。

So probably what you are actually looking for is an array of pointers, as suggested in another answer. 因此,您可能实际上正在寻找的是一个指针数组,如另一个答案所建议。 If so, you have to re-write this program. 如果是这样,您必须重新编写该程序。

This: 这个:

struct router routers[ARRAY_SIZE] = {0};

means routers is an array of ARRAY_SIZE structures. 表示routersARRAY_SIZE结构的数组。 Not pointers to structures, which is what this: 不是指向结构的指针,这是什么:

routers[new_router->id] = new_router; 

is trying to assign into one of the elements. 正在尝试分配给元素之一。

If make_router() is dynamically allocating the memory, the fix is probably to change the array declaration into an array of pointers: 如果make_router()是动态分配内存,则解决方法可能是将数组声明更改为指针数组:

struct router * routers[ARRAY_SIZE];
              ^
              |
             way
          important

EDIT : Of course, I assumed that there was an actual declaration of struct router somewhere that you just omitted. 编辑 :当然,我认为您刚刚省略的某个地方有一个struct router的实际声明。 Might be a good idea to include it, just for completeness' sake. 仅出于完整性考虑,将其包括在内可能是一个好主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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