(: Pointer to struct withing pointer to struct :)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Zero

    (: Pointer to struct withing pointer to struct :)

    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?

  • Daniel Fischer

    #2
    Re: (: Pointer to struct withing pointer to struct :)

    On Thu, 17 Nov 2005 08:02:23 -0800, Zero wrote:
    [color=blue]
    > 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?[/color]

    No. You need to request it separately:

    pPerson->MyInfos = malloc(sizeof(s truct Infos))
    [color=blue]
    > Second question:
    > How do I write values inside the second structure Infos?[/color]

    pPerson->MyInfos->Groesse etc.



    Daniel

    Comment

    • Christopher Benson-Manica

      #3
      Re: (: Pointer to struct withing pointer to struct :)

      Daniel Fischer <spam@erinye.co m> wrote:
      [color=blue]
      > pPerson->MyInfos = malloc(sizeof(s truct Infos))[/color]

      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)cybers pace.org | don't, I need to know. Flames welcome.

      Comment

      • bitshadow

        #4
        Re: (: Pointer to struct withing pointer to struct :)

        Daniel Fischer wrote:[color=blue]
        > On Thu, 17 Nov 2005 08:02:23 -0800, Zero wrote:
        >[color=green]
        > > 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?[/color]
        >
        > No. You need to request it separately:
        >
        > pPerson->MyInfos = malloc(sizeof(s truct Infos))
        >
        >[/color]

        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[color=blue][color=green]
        >> struct Person[/color][/color]
        {
        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.

        Comment

        • Keith Thompson

          #5
          Re: (: Pointer to struct withing pointer to struct :)

          "bitshadow" <caroundw5h@yah oo.com> writes:[color=blue]
          > Daniel Fischer wrote:[color=green]
          >> On Thu, 17 Nov 2005 08:02:23 -0800, Zero wrote:
          >>[color=darkred]
          >> > 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?[/color]
          >>
          >> No. You need to request it separately:
          >>
          >> pPerson->MyInfos = malloc(sizeof(s truct Infos))
          >>[/color]
          >
          > 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[color=green][color=darkred]
          >>> struct Person[/color][/color]
          > {
          > 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.[/color]

          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_Keit h) kst-u@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.

          Comment

          • Richard Heathfield

            #6
            Re: (: Pointer to struct withing pointer to struct :)

            bitshadow said:
            [color=blue]
            > Daniel Fischer wrote:[color=green]
            >> On Thu, 17 Nov 2005 08:02:23 -0800, Zero wrote:
            >>[color=darkred]
            >> > 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?[/color]
            >>
            >> No. You need to request it separately:
            >>
            >> pPerson->MyInfos = malloc(sizeof(s truct Infos))[/color]
            >
            > are you serious?? you mean C doesnt allocate memory for the struct
            > nested in person??[/color]

            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

            email: rjh at above domain (but drop the www, obviously)

            Comment

            • bitshadow

              #7
              Re: (: Pointer to struct withing pointer to struct :)

              [color=blue]
              > 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.)
              >[/color]
              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.

              Comment

              • bitshadow

                #8
                Re: (: Pointer to struct withing pointer to struct :)

                > > Second question:[color=blue][color=green]
                > > How do I write values inside the second structure Infos?[/color]
                >
                > pPerson->MyInfos->Groesse etc.
                >
                >[/color]
                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(n um_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,cl m_count,info_nd x,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?

                Comment

                • Keith Thompson

                  #9
                  Re: (: Pointer to struct withing pointer to struct :)

                  "bitshadow" <caroundw5h@yah oo.com> writes:[color=blue][color=green]
                  >> 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.)
                  >>[/color]
                  > 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.[/color]

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

                  --
                  Keith Thompson (The_Other_Keit h) kst-u@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.

                  Comment

                  • Ryan Wang

                    #10
                    Re: (: Pointer to struct withing pointer to struct :)

                    "bitshadow" <caroundw5h@yah oo.com>
                    ??????:11322838 65.243026.31155 0@f14g2000cwb.g ooglegroups.com ...[color=blue][color=green][color=darkred]
                    >> > Second question:
                    >> > How do I write values inside the second structure Infos?[/color]
                    >>
                    >> pPerson->MyInfos->Groesse etc.
                    >>
                    >>[/color]
                    > 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(n um_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;[/color]

                    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.
                    [color=blue]
                    >
                    > and when i read out whats at that location using:
                    >
                    > printf("record[%ld].clm[%d].info[%d]=%c\n",
                    > record_count,cl m_count,info_nd x,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;[/color]

                    So does it. When an address what to get its value. It can use " * "
                    or "[ ]".
                    [color=blue]
                    >
                    > 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?
                    >[/color]


                    Comment

                    • Keith Thompson

                      #11
                      Re: (: Pointer to struct withing pointer to struct :)

                      "Ryan Wang" <wangruiming@wi ndics1.com> writes:
                      [...][color=blue]
                      > 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.[/color]

                      This is explained in section 6 of the C FAQ.

                      --
                      Keith Thompson (The_Other_Keit h) kst-u@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.

                      Comment

                      • bitshadow

                        #12
                        Re: (: Pointer to struct withing pointer to struct :)

                        Keith Thompson wrote:[color=blue]
                        > "Ryan Wang" <wangruiming@wi ndics1.com> writes:
                        > [...][color=green]
                        > > 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.[/color]
                        >
                        > This is explained in section 6 of the C FAQ.[/color]

                        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.

                        Comment

                        • bitshadow

                          #13
                          Re: (: Pointer to struct withing pointer to struct :)

                          Keith Thompson wrote:[color=blue]
                          > "Ryan Wang" <wangruiming@wi ndics1.com> writes:
                          > [...][color=green]
                          > > 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.[/color]
                          >
                          > This is explained in section 6 of the C FAQ.[/color]

                          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.

                          Comment

                          • bitshadow

                            #14
                            Re: (: Pointer to struct withing pointer to struct :)


                            Keith Thompson wrote:[color=blue]
                            > "Ryan Wang" <wangruiming@wi ndics1.com> writes:
                            > [...][color=green]
                            > > 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.[/color]
                            >
                            > This is explained in section 6 of the C FAQ.[/color]

                            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.

                            Comment

                            • Keith Thompson

                              #15
                              Re: (: Pointer to struct withing pointer to struct :)

                              "bitshadow" <caroundw5h@yah oo.com> writes:[color=blue]
                              > Keith Thompson wrote:[color=green]
                              >> "Ryan Wang" <wangruiming@wi ndics1.com> writes:
                              >> [...][color=darkred]
                              >> > 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.[/color]
                              >>
                              >> This is explained in section 6 of the C FAQ.[/color]
                              >
                              > 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.[/color]

                              Sure. What was the question?

                              --
                              Keith Thompson (The_Other_Keit h) kst-u@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.

                              Comment

                              Working...