[英]Regarding forward declaration in c
typedef struct treeNodeListCell {
treeNode *node;
struct treeNodeListCell *next;
}treeNodeListCell;
typedef struct treeNode{
imgPos position;
treeNodeListCell *next_possible_positions;
}treeNode;
typedef struct segment{
treeNode *root;
}Segment;
I'm quite confused about forward declaration on the struct above,what is the way to use current declaration? 我对上述结构的前向声明很困惑,使用当前声明的方式是什么?
So, from your example code, I understand you want to use typedef
s for your struct
s and you need forward declarations. 因此,从您的示例代码中,我了解到您想对您的
struct
使用typedef
, 并且您需要前向声明。 The most straight forward (sic) way would be like this: 最简单的方法是这样的:
typedef struct treeNode treeNode;
typedef struct treeNodeListCell treeNodeListCell;
typedef struct segment segment;
struct treeNodeListCell {
treeNode *node;
treeNodeListCell *next;
};
struct treeNode {
imgPos position;
treeNodeListCell *next_possible_positions;
};
struct segment {
treeNode *root;
};
Be careful when you use an older standard than c11 . 当您使用比c11更旧的标准时请多加注意 。 In that case, repeating the
typedef
is not allowed, so any forward-declaration in a different header must look like this 在这种情况下,不允许重复
typedef
,因此在不同标头中的任何前向声明必须看起来像这样
struct treeNode;
and then use struct treeNode
instead of treeNode
to refer to the type. 然后使用
struct treeNode
而不是treeNode
来引用类型。
With c11 , this restriction is finally gone and you can repeat a typedef
if the defined type is the same. 使用c11 ,该限制终于消失了,如果定义的类型相同,则可以重复
typedef
。
You should write 你应该写
typedef struct treeNodeListCell {
struct treeNode *node;
^^^^^^^^^^^^^^^
struct treeNodeListCell *next;
}treeNodeListCell;
In this case the type name struct treeNode
is forward declared. 在这种情况下,将向前声明类型名称
struct treeNode
。
If the declaration looks like 如果声明看起来像
typedef struct treeNodeListCell {
treeNode *node;
^^^^^^^^
struct treeNodeListCell *next;
}treeNodeListCell;
then the compiler can not know what treeNode
means. 则编译器无法知道
treeNode
含义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.