在自身内部定义结构对象的问题 [英] Problem in defining object of a structure inside itself

查看:33
本文介绍了在自身内部定义结构对象的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有效:

struct LD__32
{
   struct LD__32 *ld;
};

但这不会:

struct LD_32
{
   struct LD_32 ld;
};

这是为什么?我正在将它编译为 pmg 猜测的 c++ 代码.已编辑

Why is this? I was compiling it as c++ code as pmg guessed. edited

推荐答案

C 中的结构不能包含类型不完整的成员.

A structure in C cannot contain a member with incomplete type.

在后一种情况下,您不能在 LD_32 定义中定义 LD_32,因为结构 LD_32 尚未在那一点.

In the latter case you can't have LD_32 ld; defined inside the LD_32 definition because the struct LD_32 is not already defined at that point.

查看containts关于C中结构的

第 6.7.2.1/2 节

Section 6.7.2.1/2

结构或联合不应包含不完整或函数类型的成员(因此,结构不应包含自身的实例,但可以包含指向自身实例的指针),但具有多个命名成员的结构的最后一个成员可能具有不完整的数组类型;这样的结构(以及任何包含,可能递归地包含这种结构的成员的联合)不应是结构的成员或数组的元素.

A structure or union shall not contain a member with incomplete or function type (hence, a structure shall not contain an instance of itself, but may contain a pointer to an instance of itself), except that the last member of a structure with more than one named member may have incomplete array type; such a structure (and any union containing, possibly recursively, a member that is such a structure) shall not be a member of a structure or an element of an array.

这篇关于在自身内部定义结构对象的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆