如图三个结构体,其中Student结构体要用到Book结构体,Book结构体又要用到Comment结构体,Comment结构体又要用到Student结构体,这种情况该如何处理?
用指针, 对于交叉定义没有其它办法. 注意不要内存泄漏.
#include <stdlib.h>
struct book;
typedef struct
{
struct book *b;
} student;
typedef struct
{
student s;
} comment;
typedef struct
{
comment c;
} book;
int main()
{
student s;
s.b = malloc(sizeof(student) * 10);
comment c;
book b;
free(s.b);
return 0;
}
结构体嵌套,你的思路是对的
但是要注意定义的先后关系,帖出完整的代码才知道为什么报错。