为什么结构不能有一个与自身类型相同的成员? [英] Why can't a struct have a member that is of the same type as itself?

查看:22
本文介绍了为什么结构不能有一个与自身类型相同的成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在理解这个结构发生了什么(来自 Deitel 的C for Programmers 一书).

I'm stuck on understanding what's happening with this struct (from C for Programmers a Deitel book).

书中说:结构不能包含自身的实例.例如,struct employee 类型的变量不能在struct employee 的定义中声明.但是,可以包含指向 struct employee 的指针."

The book says, "A structure cannot contain an instance of itself. For example, a variable of type struct employee cannot be declared in the definition for struct employee. A pointer to struct employee, however, may be included."

然后给出以下示例:

struct employee2 {
   char firstName[ 20 ];

   char lastName[ 20 ];

   unsigned int age;

   struct employee2 *ePtr;
};

我不明白这是在做什么,我不明白没有 2 的对 struct employee 的引用.

I don't understand what this is doing, and I don't understand the reference to struct employee without the 2.

struct employee2 *ePtr 如何知道 struct employee 或者我是不是在这里?

How does struct employee2 *ePtr know about struct employee or am I off basis here?

推荐答案

一个更有意义的例子可能是

A more meaningful example might be

struct employee2* manager;

请注意,删除 * 意味着 C 编译器必须布置顶层员工所需的 44 个(左右)字节,然后为下一个内部员工布置另外 44 个字节,然后是44 给下一个内部员工,然后 44 给下一个内部员工……等等.不用说这是一个编译错误.

Note that to remove the * means the C compiler must lay out the 44 (or so) bytes needed for the top-level employee, then another 44 bytes for the next inner employee, then the 44 for the next next inner employee, then 44 for the next next next inner employee... and so forth. Needless to say this is a compile error.

此外,这种不可能的结构将迫使他们成为不同员工,并且要求在您创建任何员工时创建所有经理,这些经理必须不为空,并且是不同的. 这意味着你不能有 CEO,而根据你的实现,CEO 的经理可以是 NULL 或她自己.这也使得在不从员工系统中删除记录(即解雇员工)并重新创建记录(招聘)的情况下更换经理变得相当不可能,这也需要撤销建筑物访问权限、计算机访问权限等.我要说的是,没有指针是一种非常非常糟糕的模拟现实世界中发生的事情的方式.

Furthermore this impossible structure would force them to all be distinct employees, and would require that when you create any employee you create all the managers, which have to be not null, and distinct. This means you can't have a CEO, whereas with a pointer the CEO's manager could be NULL or herself, depending on your implementation. It also makes it rather impossible to change managers without deleting a record from the employee system (i.e. firing an employee) and recreating it (hiring), which also requires revoking building access, computer access, and so forth. My point in saying this is that to not have a pointer is a really, really bad way to model what's going on in the real world.

然而,C 编译器可以为员工布局 44 个字节,然后为员工经理的地址布局 4 个字节,这将依次指向 44+4 个字节,if 它不是空的.请注意,这些不一定是不同的字节 - 也许员工是她自己的经理(您的业务逻辑应该禁止这样做,但是嘿,C 关心什么).

However the C compiler can lay out 44 bytes for the employee then 4 for the address of the employee's manager, which will in turn point to 44+4 bytes, if it's not null. Note these aren't necessarily distinct bytes - perhaps an employee is her own manager (your business logic should prohibit this but hey, what does C care).

一个较低级别的例子是一个链表,它更像是这样的:

A lower-level example is a linked list, which goes something more like this:

typedef struct {
    int data;
    node* next;
} node;

但是,同样的想法.除非您准备好一次创建所有无穷大的不同节点,否则这将不起作用.链表会以NULL值结尾,可以用指针来表达,但不是一个不能为空的结构体,因为它需要占用内存.

But again, same idea. Unless you have all infinity distinct nodes ready to create at once, this won't work. Linked lists will end in a NULL value, which is possible to express with a pointer, but not a structure that can't be null since it needs to take memory.

无论如何,指针是一个结构引用另一个结构的方式,而无需再次物理布置内存.C 是一种低级语言,但如果您学会从编译器的角度思考,一些更高级的概念将是有意义的.例如,删除 * 也意味着员工拥有"她的经理.这从现实世界的角度来看没有意义,从内存管理的角度来看也没有意义.(虽然,从某种意义上说,父母可以拥有孩子……这不是一个完美的类比.)

Anyway pointers are ways for one structure to refer to another structure without physically laying out the memory again. C is a low level language but if you learn to think from the point of view of the compiler some higher level concepts will make sense. For instance, to delete the * would also mean that an employee "owns" her manager. This doesn't make sense from a real-world perspective and doesn't make sense from a memory management perspective. (Although, there are senses in which parents can own children... this isn't a perfect analogy here.)

这篇关于为什么结构不能有一个与自身类型相同的成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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