为什么不能一个结构有一个部件,它是相同的类型本身的? [英] Why can't a struct have a member that is of the same type as itself?

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

问题描述

我卡在了解什么是这个结构(从℃,程序员一书的Deitel)发生。

书中说:一个结构不能包含自身的实例,例如,类型的变量结构员工不能在定义上声明了结构员工。在struct员工的指针,但是,可能被纳入。<​​/ p>

然后,它给出了下面的示例:

 结构和Employee2 {
   焦炭的firstName [20];   焦炭的lastName [20];   unsigned int类型的年龄;   结构和Employee2 * EPTR;
};

我不明白这是什么做的,我不明白,没有2参照结构员工

如何结构和Employee2 * EPTR 了解结构员工或我在这里过的基础?


解决方案

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

 结构和Employee2 *经理;

请注意,要删除 * 表示C编译器必须铺陈44(左右)字节所需的顶层雇员,然后另一个44字节下一内部雇员,则44为下一个下内雇员,则对于下一个下一个下内雇员44 ...等等。不用说,这是一个编译错误。

此外,这是不可能的结构将迫使他们都是的不同的的员工,并且需要,当你创建你的任何员工创建的所有管理者,这必须是不为空,且不同的这意味着你不能有一个CEO,而用一个指针CEO的经理可能是 NULL 或她自己,这取决于您的实现。这也使得它,而不可能改变的管理者不从员工系统中删除一条记录(即解雇员工),并重新创建它(聘用),这也要求撤销楼宇门禁,电脑上网,等等。我在说这一点是不有一个指针是一个非常,非常糟糕的方式来模拟发生了什么事在现实世界中。

然而,C编译器可以制定出44个字节为员工则对于的地址的员工的经理4,这将反过来点44 + 4个字节,的如果这不是空。注意,这些不一定是不同的字节 - 或许是员工自己的经理(你的业务逻辑应该禁止这种但是,嘿,什么是Ç护理)

一个低级别的例子是一个链表,肚里更多的东西是这样的:

  typedef结构{
    int数据;
    节点*接下来的;
}节点;

但同样,相同的想法。除非你有准备一次建立所有无限不同的节点,这是不行的。链表将在 NULL 价值,这是可能的前preSS用指针结束,而不是因为它需要执行一些不能为空的结构内存。

反正指针方式对于一个结构的参考的无需物理上再立了另一个内存结构。 C是一种低级语言,但如果你要学会但从编译器的一些更高层次的概念才有意义的角度思考。例如,要删除 * 还意味着,员工拥有她的经理。这并不现实世界的角度看来有意义的,不从存储管理的角度看是有意义的。 (虽然,也有是父母自己的孩子......这是不是一个完美的比喻在这里的感觉。)

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

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."

It then gives the following example:

struct employee2 {
   char firstName[ 20 ];

   char lastName[ 20 ];

   unsigned int age;

   struct employee2 *ePtr;
};

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

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

解决方案

A more meaningful example might be

struct employee2* manager;

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.

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.

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;

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.

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天全站免登陆