结构体中的递归定义

来源:互联网 发布:chinanet登陆软件 编辑:程序博客网 时间:2024/06/02 02:04
# include <stdio.h>typedef struct binode{    int a;    struct binode *l;}binode;int main(){int a;int *p;binode s;printf("%d %d",sizeof(a),sizeof(s));return 0;}
结果正确:
4 8


# include <stdio.h>

typedef struct binode{
    int a;
    struct binode p;

}binode;
int main()
{
int a;
int *p;
binode s;
printf("%d %d",sizeof(a),sizeof(s));
return 0;
}


系统报错:
Line 5: error: field 'p' has incomplete typecompilation terminated due to -Wfatal-errors.


通过以上测试可以看出。结构体在递归定义的时候可以定义本身,但是需要用指针,并且大小和结构体中的其他成员有关系。
如果用变量会出错,

0 0
原创粉丝点击