(: Pointer to struct withing pointer to struct :)

Z

Zero

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?
 
D

Daniel Fischer

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
 
C

Christopher Benson-Manica

Daniel Fischer said:
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().
 
B

bitshadow

Daniel said:
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 {
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.
 
K

Keith Thompson

bitshadow said:
Daniel said:
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{
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.
 
R

Richard Heathfield

bitshadow said:
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.
 
B

bitshadow

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

bitshadow

Second question:
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?
 
K

Keith Thompson

bitshadow said:
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.
 
R

Ryan Wang

bitshadow said:
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 "[ ]".
 
K

Keith Thompson

Ryan Wang said:
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.
 
B

bitshadow

Keith said:
Ryan Wang said:
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.
 
B

bitshadow

Keith said:
Ryan Wang said:
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.
 
B

bitshadow

Keith said:
Ryan Wang said:
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.
 
K

Keith Thompson

bitshadow said:
Keith said:
Ryan Wang said:
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?
 
B

bitshadow

Keith said:
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?
 
B

Barry Schwarz

Keith said:
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>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,173
Messages
2,570,938
Members
47,473
Latest member
pioneertraining

Latest Threads

Top