结构体嵌套自己

  struct Student{
int id;
float stor;
};

struct scool{
int id;
int count;
Student A; //嵌套另一个结构体,应该没问题
};

struct scool{
int id;
int count;
scool A; //嵌套自身,出现问题!
};


struct scool{
int id;
int count;
scool *A; //定义为指针,问题消失!
};

结构体不能嵌套(自己),只能通过指针的方式。假如定义了一个结构 体:struct test {int a, float b ,test c}; 在这种定义下 c 结构体变量实际是不法定义的。定义一个变量实际上是要在内存分配一个空间,这样定义无法分配一个合适的空间大小。假如改为:struct test {int a, float b ,test *c};  c变量是一个指针,那么就可以得到一个确切长度的空间分配。由此在链表中我们通常都是定一个指向结构体自身指针变量。 

posted @ 2012-02-22 16:33  yarpee  阅读(1163)  评论(0编辑  收藏  举报