【问题标题】:Accessing values in array of pointer inside of a struct访问结构内指针数组中的值
【发布时间】:2018-10-24 19:50:23
【问题描述】:

我正在尝试访问结构内部结构指针数组中结构成员的值。所以我有一个结构 Room,它包含一个指向其他 Room 的指针数组,称为 outboundConnection[]。我不断收到错误

错误:在不是结构或联合的东西中请求成员“名称” printf("连接 %i: %s", i, x.outboundConnections[i].name);

我的结构是这样设置的:

typedef struct
{
    char* name; //Name of the room
    char type; //Type of room
    int numOutboundConnections; //Number of rooms connected
    int isSelected;; // 0 means not selected, 1 means selected 
    struct Room *outboundConnections[6]; //Pointers to rooms connected
} Room;
// Room constructor used for filling roomBank
Room room_init(char* name, int s, int c)
{
    Room temp;
    temp.name = calloc(16, sizeof(char));
    strcpy(temp.name, name);
    temp.isSelected = s;
    temp.numOutboundConnections = c;
    return temp;
}

我正在使用此函数将连接添加到 outboundConnections 数组:

void ConnectRoom(Room *x, Room *y)
{
    (*x).outboundConnections[(*x).numOutboundConnections] = malloc(sizeof(Room));
    (*x).outboundConnections[(*x).numOutboundConnections] = y;
    (*x).numOutboundConnections++;
    (*y).outboundConnections[(*y).numOutboundConnections] = malloc(sizeof(Room));
    (*y).outboundConnections[(*y).numOutboundConnections] = x;
    (*y).numOutboundConnections++;
}

我在获取 outboundConnections 数组中的名称结构成员时遇到问题。

printf("Connection %i: %s", i, x.outboundConnections[i].name);

我尝试过使用 ->name 和 (*x.outboundConnections[i]).name。我想知道我是否正确地将 Rooms 分配给 outboundConnections 数组,或者我的问题是我如何尝试访问成员变量。

【问题讨论】:

    标签: c


    【解决方案1】:

    outboundConnections 是一个指针数组,所以outboundConnections[i] 是一个Room *。此外,x 也是一个指针。你应该使用->(而不是.)来访问它的成员:

    x->outboundConnections[i]->name
    

    【讨论】:

    • 我试过了,又遇到了另一个错误:'->' 的类型参数无效(有'Room')
    • 这个printf 出现在哪里。在这种情况下,x 是什么?
    • 它在一个函数中,我试图在 outboundConnections 中打印出 Room 结构的名称成员变量。这是函数:void testConnections(Room x) { int i; for(i=0; i<6; i++) { printf("Connection %i: %s", i, x->outboundConnections[i]->name); } }
    【解决方案2】:

    首先,正如其他人指出的那样,将 (*x). 符号替换为 x->

    但更重要的是,当你这样做时:

    (*x).outboundConnections[(*x).numOutboundConnections] = malloc(sizeof(Room));
    (*x).outboundConnections[(*x).numOutboundConnections] = y;
    

    您刚刚泄露了sizeof(Room) 字节。如果您要存储指向Room 的指针,则无需调用malloc。只有在存储Room副本 时才需要它。因此,如果您要存储指针,您的代码应该如下所示:

    x->outboundConnections[x->numOutboundConnections] = y;
    

    或者,如果您要存储Room 的副本,则使用这个:

    x->outboundConnections[x->numOutboundConnections] = malloc(sizeof(Room));
    *x->outboundConnections[x->numOutboundConnections] = *y;
    

    【讨论】:

      【解决方案3】:

      outboundConnections 是一个指针数组,因此您必须取消对指针的引用才能访问name 成员。

      printf("Connection %i: %s", i, x->outboundConnections[i]->name);
      

      在您编写(*x).something 的所有情况下,您都可以将其简化为x->something

      另一个问题是结构本身的声明。你有:

      struct Room *outboundConnections[6]; 
      

      但您从未声明过struct Room。你声明了一个匿名结构,然后给它一个typedef Room。您需要将声明更改为:

      typedef struct Room
      {
          char* name; //Name of the room
          char type; //Type of room
          int numOutboundConnections; //Number of rooms connected
          int isSelected;; // 0 means not selected, 1 means selected 
          struct Room *outboundConnections[6]; //Pointers to rooms connected
      } Room;
      

      【讨论】:

      • 我尝试了这个,得到另一个错误错误:'->' 的无效类型参数(有'Room')
      • 我不明白。 xRoom*,而不是 Room
      • 你没有做temp->,是吗? temp 是一个结构,而不是一个指针。
      • 您需要回到您的教科书或教程并查看有关结构和指针的部分,以便您知道何时使用.->
      • 你没有在任何地方声明struct Room
      猜你喜欢
      • 2021-09-13
      • 1970-01-01
      • 2011-08-10
      • 2012-01-30
      • 1970-01-01
      • 2017-01-16
      • 1970-01-01
      • 1970-01-01
      • 2013-06-11
      相关资源
      最近更新 更多