快速的问题,我如何转发声明以下几点 treeNodeListCell
结构。
我试着写了 struct treeNodeListCell
前的结构,但代码还是不能编译。
谁有办法?
struct treeNodeListCell;
typedef struct _treeNode {
treeNodeListCell *next_possible_positions;
} treeNode;
typedef struct _treeNodeListCell {
treeNode *node;
struct _treeNodeListCell *next;
} treeNodeListCell;
P.S.:我是第一次在stackoverflow上提问,请告诉我在写问题时有什么可以改进的地方。
这是我在stackoverflow上的第一个问题,所以请告诉我在写问题时有什么可以改进的地方。
先谢谢你:)
你 可以 申报 struct
但当你这样做时,你需要使用 struct
关键字与向前声明的 struct
标签。
struct _treeNodeListCell;
typedef struct _treeNode {
struct _treeNodeListCell *next_possible_positions;
} treeNode;
typedef struct _treeNodeListCell {
treeNode *node;
struct _treeNodeListCell *next;
} treeNodeListCell;
另一种选择是预先声明的 typedef
. C允许你 typedef
一个不完整的类型,也就是说,你可以用 typedef
在定义结构之前,您可以在结构定义中使用typedef。这样你就可以在结构体定义中使用typedef。
typedef struct _treeNodeListCell treeNodeListCell;
typedef struct _treeNode {
treeNodeListCell *next_possible_positions;
} treeNode;
struct _treeNodeListCell {
treeNode *node;
treeNodeListCell *next;
};
如果您想在不改变结构的情况下使用问题中的结构,您所需要的是 typedef
前的结构定义。
typedef struct _treeNodeListCell treeNodeListCell;
typedef struct _treeNode {
treeNodeListCell *next_possible_positions;
} treeNode;
typedef struct _treeNodeListCell {
treeNode *node;
struct _treeNodeListCell *next;
} treeNodeListCell;
您不能省略 struct
在C语言中。
你应该使用
struct treeNodeListCell *next_possible_positions;
而不是
treeNodeListCell *next_possible_positions;
快速的问题,我如何转发声明以下几点
treeNodeListCell
结构。
你不需要这样做。
首先,您必须区分通过标签识别结构类型和通过 typedef
ed别名。 特别是,你要明白 typedef
是 可有可无. 当您使用它来定义结构类型的别名时,可能会更清楚地将它与 typedef
的声明。
这里是您的声明,没有任何 typedef
:
struct _treeNode {
struct _treeNodeListCell *next_possible_positions;
};
struct _treeNodeListCell {
struct _treeNode *node;
struct _treeNodeListCell *next;
};
以下列方式表示的结构类型不需要前向声明 struct <tag>
形式。
你也可以添加typedefs。 它们可以与上面的定义相关联,通过添加的 typedef
关键词和一个或多个标识符,也可以像我之前推荐的那样,干脆把它们分开写。
typedef struct _treeNode treeNode;
typedef struct _treeNodeListCell treeNodeListCell;
我个人认为 typedefs 被过度使用了。 我通常不会定义 typedef
我的结构和联合类型的别名。
但是 如果你真的想这样做,那么你可以声明一个不完整类型的类型定义,比如一个尚未定义的结构类型。 这是一个常规声明,而不是正向声明,但它允许你在结构的定义中使用别名,我认为这就是你的目的。
typedef struct _treeNode treeNode;
typedef struct _treeNodeListCell treeNodeListCell;
struct _treeNode {
treeNodeListCell *next_possible_positions;
};
struct _treeNodeListCell {
treeNode *node;
treeNodeListCell *next;
};
事实上,从C11开始,你可以写多个相同的声明。typedef
的名称,只要它们都定义了这个名称来标识同一类型。 可以利用这个规定,让问题中提出的typedef结构声明能够编译。 注意好,指定同一类型并不要求该类型必须是 表达 同样的方式。 由于这应该是一个练习,我将把剩下的几个细节留给你们去解决。