在定义结构时避免“重新定义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; }; 

更新:愚蠢,错误的复制粘贴导致此标头被包含在另一个文件中两次。 我是C的新手,并认为它必须与文件中的结构两次有关。 谢谢@Kevin Ballard的提醒。

这是为什么需要标头/包含警卫的一个很好的例子:

 #ifndef MY_HEADER_FILE #define MY_HEADER_FILE typedef struct a A; typedef struct b B; /* ... */ #endif 

现在你已经添加了分号,我的代码中没有错误。 您也可以像这样向前声明类型:

 struct b; struct c; typedef struct a { struct b* list; struct a* parent; } A; typedef struct b { A* current; struct b* next; } B; typedef struct c { A* current; A* root; } C; 

你的方式很好,避免键入struct几次。