问题描述
大家好!
我有以下代码:
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?
推荐答案
pPerson-> MyInfos-> Groesse等
Daniel
pPerson->MyInfos->Groesse etc.
Daniel
首选
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.
否。您需要单独申请:
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
{
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的指针:)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!