Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > (: Pointer to struct withing pointer to struct :)

Reply
Thread Tools

(: Pointer to struct withing pointer to struct :)

 
 
Zero
Guest
Posts: n/a
 
      11-17-2005
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?

 
Reply With Quote
 
 
 
 
Daniel Fischer
Guest
Posts: n/a
 
      11-17-2005
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
 
Reply With Quote
 
 
 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      11-17-2005
Daniel Fischer <(E-Mail Removed)> 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.
 
Reply With Quote
 
bitshadow
Guest
Posts: n/a
 
      11-18-2005
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.

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      11-18-2005
"bitshadow" <(E-Mail Removed)> 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) http://www.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      11-18-2005
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)
 
Reply With Quote
 
bitshadow
Guest
Posts: n/a
 
      11-18-2005

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

 
Reply With Quote
 
bitshadow
Guest
Posts: n/a
 
      11-18-2005
> > 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?

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      11-18-2005
"bitshadow" <(E-Mail Removed)> 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) (E-Mail Removed) <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.
 
Reply With Quote
 
Ryan Wang
Guest
Posts: n/a
 
      11-18-2005
"bitshadow" <(E-Mail Removed)>
??????:(E-Mail Removed) 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?
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Struct pointer vs. struct array pointer aleksa C Programming 16 02-20-2013 08:20 PM
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
passing pointer->struct->pointer->struct to function. .. ?? beetle C Programming 2 01-25-2005 06:08 PM
Options for viewing word docs and editing from withing asp.net =?Utf-8?B?dHRob21wc29uMjAwNw==?= ASP .Net 0 09-20-2004 03:45 PM
Problems with asp:placeholder nested withing datalist Chris Ericoli ASP .Net 0 10-30-2003 06:24 PM



Advertisments