C11:匿名结构的TypeDEF与成员的指针一起工作吗?
这是我通常会为C中的链接列表定义一个节点:
typedef struct _Node {
int value;
struct _Node * next;
} Node;
通过进行一些测试,我发现我能够通过匿名结构来使该定义工作,例如:
typedef struct {
int value;
struct Node * next;
} Node;
有理由这样做吗?我觉得它应该不起作用,因为节点在struct定义中还不是一种类型。
我的假设是struct节点 *下一步是一个匿名结构本身,它仅具有指向节点的指针。但是,如果是这种情况,那么如果尚未解决Typedef,我如何在此处使用节点类型?
Here is how I would usually define a Node for a linked list in C:
typedef struct _Node {
int value;
struct _Node * next;
} Node;
With some testing, I have found that I am able to get this definition to work via an anonymous struct, like this:
typedef struct {
int value;
struct Node * next;
} Node;
Is there a reason why this works? I feel like it shouldn't work since Node isn't a type yet in the struct definition.
My hypothesis is that the struct Node * next is an anonymous struct itself that only has a pointer to a Node. But if this is the case then how am I able to use the Node type here if the typedef isn't resolved yet?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这似乎只有起作用。 “标签”名称空间和“普通标识符”的名称空间在C中完全不同。(C ++的规则略有不同)
您的成员
nonymous
点对于不完整的struct> struct> struct
点的下一个struct节点
,该节点是由同一声明向前指定的。您的
typedef
命名匿名struct
与该类型struct node
不同。因此,您的指针Next
指向编译器所知道的类型,而不是struct
。避免此类头痛的一个好策略是转发声明
struct node
和typedef
是相同的:然后
This only seems to work. The "tag" name space and the name space of "ordinary identifiers" are completely distinct in C. (C++ has slightly different rules)
Your member
next
of the anonymousstruct
points to an incompletestruct Node
that is forward-declared by that same declaration.Your
typedef
names an anonymousstruct
that is distinct from that typestruct Node
. So your pointernext
points to a type for which the compiler knows nothing more than that it is astruct
.A good strategy to avoid such headaches is to forward declare
struct Node
and thetypedef
to be the same:and then
该声明
介绍了两个结构规范。第一个是带有Typedef名称
Node
的未命名结构。第二个是命名结构struct node
。这两个结构指定词不同。
因此,例如,当您尝试将类型
节点 *
的指针分配给类型struct> struct> struct node *
的指针时,编译器将发出警告或错误。 Versa是因为有不兼容的指针类型。考虑以下演示计划。
例如,对于语句
,编译器GCC 11.2发出以下警告。
此外,您将无法放置类型
struct node *
的指针类型。struct节点
的定义未知。例如,如果您将上述演示程序附加使用语句
,那么此时编译器将发布以下错误消息,
而您可以写作
This declaration
introduces two structure specifiers. The first one is the unnamed structure with the typedef name
Node
. The second one is the named structurestruct Node
.These two structure specifiers are different.
So for example the compiler will issue a warning or an error when you will try to assign a pointer of the type
Node *
to a pointer of the typestruct Node *
or vice versa because there are used incompatible pointer types.Consider the following demonstration program.
For the statement
for example the compiler gcc 11.2 issues the following warnings
Moreover you will be unable to dereference a pointer of the type
struct Node *
because the typestruct Node
is an incomplete type. The definition ofstruct Node
is unknown.For example if you will append the above demonstration program with statement
then the compiler at this time will issue the following error message
Instead you could write for example