(:指向struct指向struct的指针:) [英] (: Pointer to struct withing pointer to struct :)

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

问题描述

大家好!


我有以下代码:

struct Infos

{

char Haarfarbe [40];

int Groesse;

};


struct Person

{

char名称[30];

char Nachname [20];

int Postleitzahl;

struct *信息MyInfos;

};


第一个问题:


当我在主要写作时:


struct Person * pPerson;

pPerson = malloc(sizeof(struct Person));


还有内存请求人物中的第二个结构?


第二个问题:

如何在第二个结构信息中写入值?

Hi everybody!

I have the following code:
struct Infos
{
char Haarfarbe[40];
int Groesse;
};

struct Person
{
char Name[30];
char Nachname[20];
int Postleitzahl;
struct * Infos MyInfos;
};

First question:

When I write in main:

struct Person *pPerson;
pPerson = malloc ( sizeof(struct Person) );

is there also memory requested for the second struct inside Person?

Second question:
How do I write values inside the second structure Infos?

推荐答案

2005年11月17日星期四08:02:23 -0800,Zero写道:
On Thu, 17 Nov 2005 08:02:23 -0800, Zero wrote:
第一个问题:

当我写在main中时:

struct Person * pPerson;
pPerson = malloc(sizeof(struct Person));

还有内存请求Person内的第二个结构吗?


否。您需要单独申请:


pPerson-> MyInfos = malloc(sizeof(struct Infos))

第二个问题:
如何在第二个结构Infos中写入值?
First question:

When I write in main:

struct Person *pPerson;
pPerson = malloc ( sizeof(struct Person) );

is there also memory requested for the second struct inside Person?
No. You need to request it separately:

pPerson->MyInfos = malloc(sizeof(struct Infos))
Second question:
How do I write values inside the second structure Infos?




pPerson-> MyInfos-> Groesse等


Daniel



pPerson->MyInfos->Groesse etc.

Daniel


Daniel Fischer< sp ** @ erinye.com>写道:
Daniel Fischer <sp**@erinye.com> wrote:
pPerson-> MyInfos = malloc(sizeof(struct Infos))
pPerson->MyInfos = malloc(sizeof(struct Infos))




首选


pPerson-> MyInfos = malloc(sizeof *(pPerson-> MyInfos));


这样,MyInfos类型的变化不会需要更改

来调用malloc()。


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。



Prefer

pPerson->MyInfos=malloc( sizeof *(pPerson->MyInfos) );

This way, a change in the type of MyInfos does not necessitate changes
to the call to malloc().

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


Daniel Fischer写道:
Daniel Fischer wrote:
On Thu,2005年11月17日08:02:23 -0800,Zero写道:
On Thu, 17 Nov 2005 08:02:23 -0800, Zero wrote:
第一个问题:

当我在main中写道:

struct Person * pPerson;
pPerson = malloc(sizeof(struct人));

是否还要为Person内的第二个结构请求内存?
First question:

When I write in main:

struct Person *pPerson;
pPerson = malloc ( sizeof(struct Person) );

is there also memory requested for the second struct inside Person?



否。您需要单独申请:

pPerson-> MyInfos = malloc(sizeof(struct Infos))



No. You need to request it separately:

pPerson->MyInfos = malloc(sizeof(struct Infos))




你认真吗?你的意思是C没有为结构分配内存

嵌套在人?我在C中不是那么好,但这有什么与他设置声明的方式有关吗



are you serious?? you mean C doesnt allocate memory for the struct
nested in person?? I''m not that good in C, but does this have anything
to do with how he''s set up his declaration

struct Person
struct Person



{

char名称[30];

char Nachname [20];

int Postleitzahl;

struct * Infos MyInfos;

};


不应该是:struct lnfos * MyInfos,在那里声明Infos是一个结构的指针

,但是那个结构,结构只是一个关键字

不是一个变量。 />


{
char Name[30];
char Nachname[20];
int Postleitzahl;
struct * Infos MyInfos;
};

shouldn''t it be: struct lnfos *MyInfos, stated there Infos is a pointer
to a struct, but what struct, struct at that point is just a keyword
not a variable.


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

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