结构内结构 [英] Struct inside struct

查看:43
本文介绍了结构内结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须创建一个 Person 并且每个 Person 都应该有一个冰箱.这是最好的方法吗?如果是这样,我做错了什么?提前致谢.

I must create a Person and each Person should have a Fridge. Is this the best way of doing it? If so what am I doing wrong? Thanks in advance.

typedef struct {
 int age;
 struct FRIDGE fridge;
} PERSON;

typedef struct {
 int number;
} FRIDGE;

FRIDGE fr;
fr.number=1;

PERSON me;
me.name=1;
me.fridge = fr;

这给了我以下错误:

错误:字段‘fridge’的类型不完整

error: field ‘fridge’ has incomplete type

推荐答案

struct FRIDGEFRIDGE 不同.

您需要在其他结构中使用 FRIDGE 类型.

You need to either use type FRIDGE in your other structure.

typedef struct {
 int age;
 FRIDGE fridge;
} PERSON;

或将您的冰箱定义为 struct FRIDGE

struct FRIDGE {
 int number;
};

此外,结构可能必须在您使用之前定义(例如在人物上方).

Also, the structure may have to be defined before you use it (e.g. above the person).

这篇关于结构内结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆