By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,459 Members | 1,385 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,459 IT Pros & Developers. It's quick & easy.

(: Pointer to struct withing pointer to struct :)

P: n/a
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?

Nov 17 '05 #1
Share this Question
Share on Google+
16 Replies


P: n/a
On Thu, 17 Nov 2005 08:02:23 -0800, Zero wrote:
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 etc.

Daniel
Nov 17 '05 #2

P: n/a
Daniel Fischer <sp**@erinye.com> wrote:
pPerson->MyInfos = malloc(sizeof(struct Infos))


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.
Nov 17 '05 #3

P: n/a
Daniel Fischer wrote:
On Thu, 17 Nov 2005 08:02:23 -0800, Zero wrote:
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))


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

{
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.

Nov 18 '05 #4

P: n/a
"bitshadow" <ca********@yahoo.com> writes:
Daniel Fischer wrote:
On Thu, 17 Nov 2005 08:02:23 -0800, Zero wrote:
> 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))


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

{
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.


It's not nested.

If a struct contains a pointer as one of its members, allocating space
for the struct doesn't allocate any additional space for whatever the
pointer might point to. That space has to be allocated separately.
(Of course, if a struct contains another struct as one of its members,
space is allocated for the nested struct, just as it is for all the
other members that make up the enclosing struct.)

As for
struct * Infos MyInfos;
that's a syntax error (and probably just a typo in the original
message). It should be
struct Infos *MyInfos;

This is why we encourage people to cut-and-paste the exact code that
they've compiled rather than just typing it in. Typos are inevitable,
and we can't guess which errors are in the original code and which
were introduced by re-typing it.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 18 '05 #5

P: n/a
bitshadow said:
Daniel Fischer wrote:
On Thu, 17 Nov 2005 08:02:23 -0800, Zero wrote:
> 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))


are you serious?? you mean C doesnt allocate memory for the struct
nested in person??


There isn't a struct nested in person. If there were, C would have allocated
memory for it.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Nov 18 '05 #6

P: n/a
It's not nested.

If a struct contains a pointer as one of its members, allocating space
for the struct doesn't allocate any additional space for whatever the
pointer might point to. That space has to be allocated separately.
(Of course, if a struct contains another struct as one of its members,
space is allocated for the nested struct, just as it is for all the
other members that make up the enclosing struct.)

Yes, you are obviously right, for some reason, i was looking and seeing
a nested structure. So the dereferencing to find the value makes sense.

Nov 18 '05 #7

P: n/a
> > Second question:
How do I write values inside the second structure Infos?


pPerson->MyInfos->Groesse etc.

if you add a value to a address like that. assuming:

typedef struct{
char info[SIZE];
}CLMS;

typedef struct{
CLMS clm[MAX_CLMS];
}PERSON;

PERSON *record;
record=malloc(num_of_records * sizeof(PERSON) );

how is that i can assign values to the struct like so:

while( (character = fgetc(filename) )!=EOF){
record[record_count].clm[clm_count].info[info_ndx] = character;

and when i read out whats at that location using:

printf("record[%ld].clm[%d].info[%d]=%c\n",
record_count,clm_count,info_ndx,record[record_count].clm[clm_count].info[info_ndx]);

i get the exact values that are there.

I'm a little confused as why i don't need to say:
(*record[record_count]).clm[clm_count].info[info_ndx] = character;
or:
record[record_count]->clm[clm_count].info[info_ndx] = character

where as in an array:

int ndx, nums[MAX];
int *ptr;
ptr = nums;

for(ndx=0; ndx <MAX; ndx++){
*(nums + ndx) = ndx * choice;

i can't just say (nums + ndx) = value;
i seem to have to specify that the value there gets this value, where
as in the first with the struct i seem to say the address gets this
value and it works. What am i missing?

Nov 18 '05 #8

P: n/a
"bitshadow" <ca********@yahoo.com> writes:
It's not nested.

If a struct contains a pointer as one of its members, allocating space
for the struct doesn't allocate any additional space for whatever the
pointer might point to. That space has to be allocated separately.
(Of course, if a struct contains another struct as one of its members,
space is allocated for the nested struct, just as it is for all the
other members that make up the enclosing struct.)

Yes, you are obviously right, for some reason, i was looking and seeing
a nested structure. So the dereferencing to find the value makes sense.


FYI, Google adds an attribution line, like "So-and-so writes:".
Please don't delete it. Thanks.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 18 '05 #9

P: n/a
"bitshadow" <ca********@yahoo.com>
??????:11**********************@f14g2000cwb.google groups.com...
> Second question:
> How do I write values inside the second structure Infos?
pPerson->MyInfos->Groesse etc.

if you add a value to a address like that. assuming:

typedef struct{
char info[SIZE];
}CLMS;

typedef struct{
CLMS clm[MAX_CLMS];
}PERSON;

PERSON *record;
record=malloc(num_of_records * sizeof(PERSON) );

how is that i can assign values to the struct like so:

while( (character = fgetc(filename) )!=EOF){
record[record_count].clm[clm_count].info[info_ndx] = character;


i thinks but not sure. record is an address ,and the '[ ]' will get the
value of the address. it is just like an array.
when define an array like "int a[10]"
here "a" is an address,but "a[2]" will reture you a int number.

and when i read out whats at that location using:

printf("record[%ld].clm[%d].info[%d]=%c\n",
record_count,clm_count,info_ndx,record[record_count].clm[clm_count].info[info_ndx]);

i get the exact values that are there.

I'm a little confused as why i don't need to say:
(*record[record_count]).clm[clm_count].info[info_ndx] = character;
or:
record[record_count]->clm[clm_count].info[info_ndx] = character

where as in an array:

int ndx, nums[MAX];
int *ptr;
ptr = nums;

for(ndx=0; ndx <MAX; ndx++){
*(nums + ndx) = ndx * choice;
So does it. When an address what to get its value. It can use " * "
or "[ ]".

i can't just say (nums + ndx) = value;
i seem to have to specify that the value there gets this value, where
as in the first with the struct i seem to say the address gets this
value and it works. What am i missing?

Nov 18 '05 #10

P: n/a
"Ryan Wang" <wa*********@windics1.com> writes:
[...]
i thinks but not sure. record is an address ,and the '[ ]' will get the
value of the address. it is just like an array.
when define an array like "int a[10]"
here "a" is an address,but "a[2]" will reture you a int number.


This is explained in section 6 of the C FAQ.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 18 '05 #11

P: n/a
Keith Thompson wrote:
"Ryan Wang" <wa*********@windics1.com> writes:
[...]
i thinks but not sure. record is an address ,and the '[ ]' will get the
value of the address. it is just like an array.
when define an array like "int a[10]"
here "a" is an address,but "a[2]" will reture you a int number.


This is explained in section 6 of the C FAQ.


Well keith can you point me to exactly where in the FAQ because i am
still a novice to C and this question plagues me. I don't believe Ryan
is correct - though well meaning - because i have a piece of code that
says other wise.
Or more kindly i know you care a C sage, if you could explain it i
would be more than appreciative. Thank you.

Nov 18 '05 #12

P: n/a
Keith Thompson wrote:
"Ryan Wang" <wa*********@windics1.com> writes:
[...]
i thinks but not sure. record is an address ,and the '[ ]' will get the
value of the address. it is just like an array.
when define an array like "int a[10]"
here "a" is an address,but "a[2]" will reture you a int number.


This is explained in section 6 of the C FAQ.


Well keith can you point me to exactly where in the FAQ because i am
still a novice to C and this question plagues me. I don't believe Ryan
is correct - though well meaning - because i have a piece of code that
says other wise.
Or more kindly i know you care a C sage, if you could explain it i
would be more than appreciative. Thank you.

Nov 18 '05 #13

P: n/a

Keith Thompson wrote:
"Ryan Wang" <wa*********@windics1.com> writes:
[...]
i thinks but not sure. record is an address ,and the '[ ]' will get the
value of the address. it is just like an array.
when define an array like "int a[10]"
here "a" is an address,but "a[2]" will reture you a int number.


This is explained in section 6 of the C FAQ.


Well keith can you point me to exactly where in the FAQ because i am
still a novice to C and this question plagues me. I don't believe Ryan
is correct - though well meaning - because i have a piece of code that
says other wise.
Or more kindly i know you care a C sage, if you could explain it i
would be more than appreciative. Thank you.

Nov 18 '05 #14

P: n/a
"bitshadow" <ca********@yahoo.com> writes:
Keith Thompson wrote:
"Ryan Wang" <wa*********@windics1.com> writes:
[...]
> i thinks but not sure. record is an address ,and the '[ ]' will get the
> value of the address. it is just like an array.
> when define an array like "int a[10]"
> here "a" is an address,but "a[2]" will reture you a int number.


This is explained in section 6 of the C FAQ.


Well keith can you point me to exactly where in the FAQ because i am
still a novice to C and this question plagues me. I don't believe Ryan
is correct - though well meaning - because i have a piece of code that
says other wise.


Sure. What was the question?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 18 '05 #15

P: n/a
Keith Thompson wrote:
Sure. What was the question?

assuming:

typedef struct{
char info[SIZE];
}CLMS;

typedef struct{
CLMS clm[MAX_CLMS];
}PERSON;

PERSON *record;
record=malloc(num_of_records * sizeof(PERSON) );

how is that i can assign values to the struct like so:

while( (character = fgetc(filename) )!=EOF){
record[record_count].clm[clm_count].info[info_ndx] = character;

and when i read out whats at that location using:

printf("record[%ld].clm[%d].info[%d]=%c\n",
record_count,clm_count,info_ndx,record[record_count].clm[clm_count].info[info_ndx]);

i get the exact values that are there.

I'm a little confused as why i don't need to say:
(*record[record_count]).clm[clm_count].info[info_ndx] = character;
or:
record[record_count]->clm[clm_count].info[info_ndx] = character

where as in an array:

int ndx, nums[MAX];
int *ptr;
ptr = nums;

for(ndx=0; ndx <MAX; ndx++){
*(nums + ndx) = ndx * choice;

i can't just say (nums + ndx) = value;
i seem to have to specify that the value there gets this value, where
as in the first with the struct i seem to say the address gets this
value and it works. What am i missing?

Nov 18 '05 #16

P: n/a
On 18 Nov 2005 15:28:34 -0800, "bitshadow" <ca********@yahoo.com>
wrote:
Keith Thompson wrote:
Sure. What was the question?
assuming:

typedef struct{
char info[SIZE];
}CLMS;

typedef struct{
CLMS clm[MAX_CLMS];
}PERSON;

PERSON *record;
record=malloc(num_of_records * sizeof(PERSON) );

how is that i can assign values to the struct like so:

while( (character = fgetc(filename) )!=EOF){
record[record_count].clm[clm_count].info[info_ndx] = character;

and when i read out whats at that location using:

printf("record[%ld].clm[%d].info[%d]=%c\n",
record_count,clm_count,info_ndx,record[record_count].clm[clm_count].info[info_ndx]);

i get the exact values that are there.

I'm a little confused as why i don't need to say:
(*record[record_count]).clm[clm_count].info[info_ndx] = character;


Look at the types. record is of type pointer to struct. [] binds
tighter that * so record[record_count] is one of the structs that
record points to. Since this is not a pointer, it is illegal to
dereference it with *.

On the other hand, since it is a structure, it has a member called
clm. This member is itself an array of structures so
record[record_count.clm[clm_count] is one of the structures in the
array. This structure has a member called info. This member is an
array of char and record[record_count.clm[clm_count].info[info_ndx] is
one of the char in that array.
or:
record[record_count]->clm[clm_count].info[info_ndx] = character

where as in an array:

int ndx, nums[MAX];
int *ptr;
ptr = nums;

for(ndx=0; ndx <MAX; ndx++){
*(nums + ndx) = ndx * choice;
Are you aware that *(nums+ndx) is identical to nums[ndx]?

i can't just say (nums + ndx) = value;
i seem to have to specify that the value there gets this value, where
as in the first with the struct i seem to say the address gets this
value and it works. What am i missing?

<<Remove the del for email>>
Nov 19 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.