在C语言中不可能做这样的事情吗?

struct room{
        //Name of the room
        char* name;
        //Type fo the room
        char* type;
        //Array of outbound connections, max of six
        struct room connections[6];
        //A counter variable for how many connections the room actually has been assigned
        int numOfConnections;
};

我正在创建一个相互连接的房间的地图,我认为每个房间跟踪其连接的房间的最简单方法是创建一个房间结构数组,然后将这些房间放在它们的房间中。
我得到一个错误,说房间数组有一个不完整的元素类型。错误出现在“struct room connections[6];”

最佳答案

为了将struct存储在自身内部,它必须是指针类型。否则,如注释中所述,这个struct将占用无限空间。下面的更改使它成为指向6struct room的指针。

struct room{
        //Name of the room
        char* name;
        //Type fo the room
        char* type;
        //Array of outbound connections, max of six
        struct room* connections[6];
        //A counter variable for how many connections the room actually has been assigned
        int numOfConnections;
};

关于c - 试图在C的房间结构定义中声明一个结构房间数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52141388/

10-13 04:49