(: Pointer to struct withing pointer to struct :)

Discussion in 'C Programming' started by Zero, Nov 17, 2005.

  1. Zero

    Zero Guest

    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?
    Zero, Nov 17, 2005
    #1
    1. Advertising

  2. 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
    Daniel Fischer, Nov 17, 2005
    #2
    1. Advertising

  3. Daniel Fischer <> 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.
    Christopher Benson-Manica, Nov 17, 2005
    #3
  4. Zero

    bitshadow Guest

    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.
    bitshadow, Nov 18, 2005
    #4
  5. "bitshadow" <> 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.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Nov 18, 2005
    #5
  6. 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)
    Richard Heathfield, Nov 18, 2005
    #6
  7. Zero

    bitshadow Guest


    > 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.
    bitshadow, Nov 18, 2005
    #7
  8. Zero

    bitshadow Guest

    > > 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?
    bitshadow, Nov 18, 2005
    #8
  9. "bitshadow" <> 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) <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.
    Keith Thompson, Nov 18, 2005
    #9
  10. Zero

    Ryan Wang Guest

    "bitshadow" <>
    ??????:...
    >> > 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?
    >
    Ryan Wang, Nov 18, 2005
    #10
  11. "Ryan Wang" <> 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) <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.
    Keith Thompson, Nov 18, 2005
    #11
  12. Zero

    bitshadow Guest

    Keith Thompson wrote:
    > "Ryan Wang" <> 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.
    bitshadow, Nov 18, 2005
    #12
  13. Zero

    bitshadow Guest

    Keith Thompson wrote:
    > "Ryan Wang" <> 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.
    bitshadow, Nov 18, 2005
    #13
  14. Zero

    bitshadow Guest

    Keith Thompson wrote:
    > "Ryan Wang" <> 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.
    bitshadow, Nov 18, 2005
    #14
  15. "bitshadow" <> writes:
    > Keith Thompson wrote:
    >> "Ryan Wang" <> 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) <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.
    Keith Thompson, Nov 18, 2005
    #15
  16. Zero

    bitshadow Guest

    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?
    bitshadow, Nov 18, 2005
    #16
  17. On 18 Nov 2005 15:28:34 -0800, "bitshadow" <>
    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>>
    Barry Schwarz, Nov 19, 2005
    #17
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Ericoli
    Replies:
    0
    Views:
    481
    Chris Ericoli
    Oct 30, 2003
  2. =?Utf-8?B?dHRob21wc29uMjAwNw==?=

    Options for viewing word docs and editing from withing asp.net

    =?Utf-8?B?dHRob21wc29uMjAwNw==?=, Sep 20, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    328
    =?Utf-8?B?dHRob21wc29uMjAwNw==?=
    Sep 20, 2004
  3. J.K. Bach
    Replies:
    0
    Views:
    346
    J.K. Bach
    Jun 15, 2005
  4. Replies:
    1
    Views:
    313
    =?Utf-8?B?Q3VydF9DIFtNVlBd?=
    Sep 9, 2005
  5. =?Utf-8?B?WUM=?=

    XSI type not supported withing web.config file

    =?Utf-8?B?WUM=?=, Apr 5, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    378
    =?Utf-8?B?WUM=?=
    Apr 5, 2006
Loading...

Share This Page