【发布时间】: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