【发布时间】:2013-05-20 10:44:20
【问题描述】:
我一直在理解这个结构发生了什么(来自C for Programmers Deitel 的书)。
书上说,“结构不能包含自身的实例。例如,struct employee 类型的变量不能在 struct employee 的定义中声明。但是,可以包含指向 struct employee 的指针。 "
然后给出下面的例子:
struct employee2 {
char firstName[ 20 ];
char lastName[ 20 ];
unsigned int age;
struct employee2 *ePtr;
};
我不明白这是在做什么,我不明白没有 2 的对 struct employee 的引用。
struct employee2 *ePtr 是如何知道struct employee 的,或者我是不是在这里?
【问题讨论】:
-
也许获取结构或结构元素地址比访问已经拥有地址的元素慢。
-
您引用的代码中没有
struct employee;只有struct employee2。 -
@jamesdlin 这本书没有引用
struct employee的代码。这就是我没有包含它的原因。 -
这是重复的,但我的答案与那里的任何答案都不同,OP 表示它很有帮助 - 所以问题应该合并而不是关闭。 stackoverflow.com/questions/588623/…
-
@nkon:你问“
struct employee2 *ePtr怎么知道struct employee”。它不知道也不需要知道;struct employee没有以任何方式参与。struct employee2是。