我有两个文件(每个文件都有 .h 和 .c)。我在头文件之一中定义以下结构:
typedef struct Client{
int fd;
struct in_addr ipaddr;
struct Player * p;
struct Client * next;
}Client;
我已经尝试了许多不同的 typedef 和 struct 的建议组合,但我仍然得到 error: redefinition of ‘struct client’
我开始认为这与两个头文件相互包含有关。
我是否正确创建了这个结构?在相互包含头文件时,我应该遵循一些基本规则吗?
最佳答案
如果您有两个相互依赖的结构,您可以转发声明其中一个。
typedef struct Client Client; // forward declaration of struct Client
typedef struct Player {
...
Client *client;
...
} Player;
struct Client{
int fd;
struct in_addr ipaddr;
Player * p;
Client * next;
};
关于c - 如何定义和 typedef 一个包含指向自身的指针的结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38710955/