"struct inside struct"中的限制

KON*_*ONI 2 c struct

有2个struct定义AA.我知道可以struct A包含一个POINTER,struct A但我不明白为什么struct A 不能包含struct A(不是一个指针)

Mik*_*ike 5

因为当你将结构放在彼此内部时,你会在那个结构中将该结构的另一个副本放入结构中.例如:

struct A {
    int q;
    int w;
};
struct B {
    int x;
    struct A y;
    int z;
};
Run Code Online (Sandbox Code Playgroud)

这将在内存中列出如下:

int /*B.*/x;
int /*A.*/q;
int /*A.*/w;
int /*B.*/z;
Run Code Online (Sandbox Code Playgroud)

但是如果你试图在其中放置一个结构:

struct A {
    int x;
    struct A y;
};
Run Code Online (Sandbox Code Playgroud)

你有一个A,它包含一个int和另一个A,它包含一个int和另一个A,现在你有一个无限数量的int.