struct inside struct [英] Struct inside struct

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

问题描述

我必须创建一个人,每个人都应该有一个冰箱。这是最好的做法吗?如果是这样我做错了什么?提前感谢。

  typedef struct {
int age;
struct FRIDGE冰箱;
} PERSON;

typedef struct {
int number;
} FRIDGE;

FRIDGE fr;
fr.number = 1;

个人
me.name = 1;
me.fridge = fr;

这给我以下错误:



< >

错误:字段冰箱不完整类型



解决方案

struct FRIDGE 是不同于 FRIDGE 的东西。



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

  typedef struct {
int age;
FRIDGE冰箱;
} PERSON;

或将您的冰箱定义为 struct FRIDGE

  struct FRIDGE {
int number;
};

此外,结构可能必须在使用之前定义/ p>

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;

This gives me the following error:

error: field ‘fridge’ has incomplete type

解决方案

struct FRIDGE is something different than FRIDGE.

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

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

or define your fridge as struct FRIDGE

struct FRIDGE {
 int number;
};

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

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

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