Bor*_*ort 1 c gcc struct typedef
我正在定义一些引用彼此的结构,并在使用它们之前键入结构,因此每个结构都"了解"其他结构(没有这个就得到了编译错误).不确定这是否必要或正确.
现在用gcc编译时,我正在"重新定义typedef"警告.什么是正确的方法去做?
typedef struct a A;
typedef struct b B;
typedef struct c C;
struct a {
B* list;
A* parent;
};
struct b {
A* current;
B* next;
};
struct c {
A* current;
A* root;
};
Run Code Online (Sandbox Code Playgroud)
更新: 愚蠢,错误的复制粘贴导致此标头被包含在另一个文件中两次.我是C的新手,并认为它必须与文件中的结构两次有关.谢谢@Kevin Ballard的提醒.
这是为什么需要标头/包含警卫的一个很好的例子:
#ifndef MY_HEADER_FILE
#define MY_HEADER_FILE
typedef struct a A;
typedef struct b B;
/* ... */
#endif
Run Code Online (Sandbox Code Playgroud)