Pointer to array of structs?

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

    Pointer to array of structs?

    Still having a few problems with malloc and pointers.

    I have made a struct. Now I would like to make a pointer an array with 4
    pointers to this struct.

    #include <stdlib.h>
    #include <stdio.h>
    typedef struct _tnode_t {
    void *content;
    struct _tnode_t *kids;
    } tnode_t;


    int main(void)
    {
    tnode_t *array;
    array = malloc(sizeof(t node_t)*4);
    array[3] = NULL;
    return 0;

    }

    As I understand "array" point to the first element of an array consisting of
    space for 4 tnode_t structs. But why can't I initialize element at index 3
    to NULL?
  • Artie Gold

    #2
    Re: Pointer to array of structs?

    Paminu wrote:[color=blue]
    > Still having a few problems with malloc and pointers.
    >
    > I have made a struct. Now I would like to make a pointer an array with 4
    > pointers to this struct.
    >
    > #include <stdlib.h>
    > #include <stdio.h>
    > typedef struct _tnode_t {
    > void *content;
    > struct _tnode_t *kids;
    > } tnode_t;
    >
    >
    > int main(void)
    > {
    > tnode_t *array;
    > array = malloc(sizeof(t node_t)*4);[/color]

    Better (and that may further illustrate the point below)

    array = malloc(sizeof *array * 4);

    showing you that you have allocated enough space to point to 4 of what
    your variable `array' points to.
    [color=blue]
    > array[3] = NULL;
    > return 0;
    >
    > }
    >
    > As I understand "array" point to the first element of an array consisting of
    > space for 4 tnode_t structs. But why can't I initialize element at index 3
    > to NULL?[/color]

    Because array[3] is a `tnode_t', not a pointer to a `tnode_t' -- and
    NULL is a pointer constant.

    If you want an array of *pointers* to tnode_t, you would write:

    tnode_t **array = malloc(sizeof *array * 4);

    and allocate enough space for a tnode_t for each element as needed.

    HTH,
    --ag
    --
    Artie Gold -- Austin, Texas
    I will (ir)regularly write about things that are important to me -- that I hope interest you. Of course, since I see most things as being political, that will be most of it.


    "If you have nothing to hide, you're not trying!"

    Comment

    • Robert Gamble

      #3
      Re: Pointer to array of structs?

      Paminu wrote:[color=blue]
      > Still having a few problems with malloc and pointers.
      >
      > I have made a struct. Now I would like to make a pointer an array with 4
      > pointers to this struct.
      >
      > #include <stdlib.h>
      > #include <stdio.h>
      > typedef struct _tnode_t {
      > void *content;
      > struct _tnode_t *kids;
      > } tnode_t;
      >
      >
      > int main(void)
      > {
      > tnode_t *array;
      > array = malloc(sizeof(t node_t)*4);
      > array[3] = NULL;
      > return 0;
      >
      > }
      >
      > As I understand "array" point to the first element of an array consisting of
      > space for 4 tnode_t structs. But why can't I initialize element at index 3
      > to NULL?[/color]

      tnode_t *array; creates a pointer to a tnode_t structure. If you want
      an array of 4 pointers to tnode_t structs you would use tnode_t
      *array[4]. If you want want to create a dynamically allocated array of
      pointers to tnode_t structures, you would want your pointer to be of
      type "pointer to pointer to tnode_t":
      tnode_t **array;

      sizeof(tnode_t) *4 is the size required to store 4 tnode_t structures.
      sizeof(tnode_t *)*4 will evaluate to the number of bytes required for 4
      pointers to tnode_t.

      Robert Gamble

      Comment

      • Paminu

        #4
        Re: Pointer to array of structs?

        Robert Gamble wrote:
        [color=blue]
        > Paminu wrote:[color=green]
        >> Still having a few problems with malloc and pointers.
        >>
        >> I have made a struct. Now I would like to make a pointer an array with 4
        >> pointers to this struct.
        >>
        >> #include <stdlib.h>
        >> #include <stdio.h>
        >> typedef struct _tnode_t {
        >> void *content;
        >> struct _tnode_t *kids;
        >> } tnode_t;
        >>
        >>
        >> int main(void)
        >> {
        >> tnode_t *array;
        >> array = malloc(sizeof(t node_t)*4);
        >> array[3] = NULL;
        >> return 0;
        >>
        >> }
        >>
        >> As I understand "array" point to the first element of an array consisting
        >> of space for 4 tnode_t structs. But why can't I initialize element at
        >> index 3 to NULL?[/color]
        >
        > tnode_t *array; creates a pointer to a tnode_t structure. If you want
        > an array of 4 pointers to tnode_t structs you would use tnode_t
        > *array[4].[/color]


        It seems to work if I do:

        int main(void)
        {
        tnode_t *array[4];
        tnode_t *test;
        test = malloc(sizeof(t node_t));
        test->content = "Test";
        test->kids = NULL;

        array[0] = test;
        array[1] = NULL;
        // etc.

        printf("%s\n", array[0]->content);

        return 0;

        }


        was that what you meant?

        Comment

        • Paminu

          #5
          Re: Pointer to array of structs?

          Robert Gamble wrote:
          [color=blue]
          > Paminu wrote:[color=green]
          >> Still having a few problems with malloc and pointers.
          >>
          >> I have made a struct. Now I would like to make a pointer an array with 4
          >> pointers to this struct.
          >>
          >> #include <stdlib.h>
          >> #include <stdio.h>
          >> typedef struct _tnode_t {
          >> void *content;
          >> struct _tnode_t *kids;
          >> } tnode_t;
          >>
          >>
          >> int main(void)
          >> {
          >> tnode_t *array;
          >> array = malloc(sizeof(t node_t)*4);
          >> array[3] = NULL;
          >> return 0;
          >>
          >> }
          >>
          >> As I understand "array" point to the first element of an array consisting
          >> of space for 4 tnode_t structs. But why can't I initialize element at
          >> index 3 to NULL?[/color]
          >
          > tnode_t *array; creates a pointer to a tnode_t structure.[/color]


          Ok so it will only work if I set array[3] equal to a struct instead of a
          pointer to a struct or NULL:

          1)
          tnode_t tt;
          tt.content = "test";
          tt.kids = NULL;
          tnode_t *array;
          array[0] = tt;


          this seems to work and make sense. But if I make:

          2)
          int *p;
          p = malloc(sizeof(i nt)*4);
          p[0] = NULL;


          this only give the warning: "warning: assignment makes integer from pointer
          without a cast".

          In 1) if I did: array[0] = NULL; I would get the error:

          "error: incompatible types in assignment".

          Why the different rules?



          Comment

          • Antonio Contreras

            #6
            Re: Pointer to array of structs?

            Paminu wrote:[color=blue]
            > Robert Gamble wrote:
            >[color=green]
            > > Paminu wrote:[color=darkred]
            > >> Still having a few problems with malloc and pointers.
            > >>
            > >> I have made a struct. Now I would like to make a pointer an array with 4
            > >> pointers to this struct.
            > >>
            > >> #include <stdlib.h>
            > >> #include <stdio.h>
            > >> typedef struct _tnode_t {
            > >> void *content;
            > >> struct _tnode_t *kids;
            > >> } tnode_t;
            > >>
            > >>
            > >> int main(void)
            > >> {
            > >> tnode_t *array;
            > >> array = malloc(sizeof(t node_t)*4);
            > >> array[3] = NULL;
            > >> return 0;
            > >>
            > >> }
            > >>
            > >> As I understand "array" point to the first element of an array consisting
            > >> of space for 4 tnode_t structs. But why can't I initialize element at
            > >> index 3 to NULL?[/color]
            > >
            > > tnode_t *array; creates a pointer to a tnode_t structure.[/color]
            >
            >
            > Ok so it will only work if I set array[3] equal to a struct instead of a
            > pointer to a struct or NULL:
            >
            > 1)
            > tnode_t tt;
            > tt.content = "test";
            > tt.kids = NULL;
            > tnode_t *array;
            > array[0] = tt;
            >
            >
            > this seems to work and make sense. But if I make:
            >
            > 2)
            > int *p;
            > p = malloc(sizeof(i nt)*4);
            > p[0] = NULL;
            >
            >
            > this only give the warning: "warning: assignment makes integer from pointer
            > without a cast".[/color]

            You still seem to be confusing pointers to types with the types they
            point. In the second code snippet you posted you declare a pointer to
            integer and then allocate memory for four integers and assign the value
            returned by malloc to your pointer. Now you have a dynamic array of
            four integers that can be accessed as p[0], p[1], [2] and p[3] or any
            equivalent way.

            In your next statement you assign a null pointer constant (NULL) to
            p[0]. This can be done and is perfectly legal, but it is implementation
            defined. Pointers can be implicitly or explicitly converted to integers
            and back in an implementation defined manner. The compiler is just
            being nice telling you that this is something strange.
            [color=blue]
            >
            > In 1) if I did: array[0] = NULL; I would get the error:
            >
            > "error: incompatible types in assignment".
            >
            > Why the different rules?[/color]

            Here the scenario is totally different. array is of type pointer to
            tnode_t. array[0] is of type tnode_t which is a struct. Now you're
            trying to assign a null pointer constant to a struct and this cannot be
            done. There's no implicit conversion between pointers and structs and
            if you think of it for a while you'll realize that there is no such
            conversion because it simply doesn't make sense.

            HTH

            Comment

            • Nils O. SelĂ„sdal

              #7
              Re: Pointer to array of structs?

              Paminu wrote:[color=blue]
              > Still having a few problems with malloc and pointers.
              >
              > I have made a struct. Now I would like to make a pointer an array with 4
              > pointers to this struct.
              >
              > #include <stdlib.h>
              > #include <stdio.h>
              > typedef struct _tnode_t {
              > void *content;
              > struct _tnode_t *kids;
              > } tnode_t;
              >
              >
              > int main(void)
              > {
              > tnode_t *array;
              > array = malloc(sizeof(t node_t)*4);
              > array[3] = NULL;
              > return 0;
              >
              > }
              >
              > As I understand "array" point to the first element of an array consisting of
              > space for 4 tnode_t structs. But why can't I initialize element at index 3
              > to NULL?[/color]

              Because NULL is a pointer value. index 3 stores a tnode_t , not a
              pointer to a tnode_t.

              You cannot store a pointer in a place that cannot hold a pointer.

              Comment

              • Rod Pemberton

                #8
                Re: Pointer to array of structs?


                "Paminu" <sdef@asd.com > wrote in message
                news:dscaig$su$ 1@news.net.uni-c.dk...[color=blue]
                > Robert Gamble wrote:
                >[color=green]
                > > Paminu wrote:[color=darkred]
                > >> Still having a few problems with malloc and pointers.
                > >>
                > >> I have made a struct. Now I would like to make a pointer an array with[/color][/color][/color]
                4[color=blue][color=green][color=darkred]
                > >> pointers to this struct.
                > >>
                > >> #include <stdlib.h>
                > >> #include <stdio.h>
                > >> typedef struct _tnode_t {
                > >> void *content;
                > >> struct _tnode_t *kids;
                > >> } tnode_t;
                > >>
                > >>
                > >> int main(void)
                > >> {
                > >> tnode_t *array;
                > >> array = malloc(sizeof(t node_t)*4);
                > >> array[3] = NULL;
                > >> return 0;
                > >>
                > >> }
                > >>
                > >> As I understand "array" point to the first element of an array[/color][/color][/color]
                consisting[color=blue][color=green][color=darkred]
                > >> of space for 4 tnode_t structs. But why can't I initialize element at
                > >> index 3 to NULL?[/color]
                > >
                > > tnode_t *array; creates a pointer to a tnode_t structure.[/color]
                >
                >
                > Ok so it will only work if I set array[3] equal to a struct instead of a
                > pointer to a struct or NULL:
                >
                > 1)
                > tnode_t tt;
                > tt.content = "test";
                > tt.kids = NULL;
                > tnode_t *array;
                > array[0] = tt;
                >
                >
                > this seems to work and make sense. But if I make:
                >
                > 2)
                > int *p;
                > p = malloc(sizeof(i nt)*4);
                > p[0] = NULL;
                >
                >
                > this only give the warning: "warning: assignment makes integer from[/color]
                pointer[color=blue]
                > without a cast".
                >
                > In 1) if I did: array[0] = NULL; I would get the error:
                >
                > "error: incompatible types in assignment".
                >
                > Why the different rules?[/color]

                Different errors.

                The pointer to an int, p, can be assigned NULL:
                p=NULL;

                Or, you can assign the int that p[0] references to zero:
                p[0]=0;

                Or, you can assign the int that p[1] references to zero:
                p[1]=0;

                etc...

                When you write:[color=blue]
                > p[0] = NULL;[/color]
                You are attempting to convert a pointer (NULL) to an int.


                Rod Pemberton


                Comment

                • Paminu

                  #9
                  Re: Pointer to array of structs?

                  "Nils O. SelÄsdal" wrote:
                  [color=blue]
                  > Paminu wrote:[color=green]
                  >> Still having a few problems with malloc and pointers.
                  >>
                  >> I have made a struct. Now I would like to make a pointer an array with 4
                  >> pointers to this struct.
                  >>
                  >> #include <stdlib.h>
                  >> #include <stdio.h>
                  >> typedef struct _tnode_t {
                  >> void *content;
                  >> struct _tnode_t *kids;
                  >> } tnode_t;
                  >>
                  >>
                  >> int main(void)
                  >> {
                  >> tnode_t *array;
                  >> array = malloc(sizeof(t node_t)*4);
                  >> array[3] = NULL;
                  >> return 0;
                  >>
                  >> }
                  >>
                  >> As I understand "array" point to the first element of an array consisting
                  >> of space for 4 tnode_t structs. But why can't I initialize element at
                  >> index 3 to NULL?[/color]
                  >
                  > Because NULL is a pointer value. index 3 stores a tnode_t , not a
                  > pointer to a tnode_t.
                  >
                  > You cannot store a pointer in a place that cannot hold a pointer.[/color]


                  Ok I guess that this is correct and identical:

                  typedef struct _tnode_t {
                  void *content;
                  struct _tnode_t *kids;
                  } tnode_t;

                  int main(void)
                  {
                  tnode_t *array;
                  array[0].kids = NULL;
                  array->kids = NULL;
                  return 0;

                  }

                  when I do this: array[i] I get a struct. When I do this: array->kids I get a
                  pointer to a struct.

                  Comment

                  • Antonio Contreras

                    #10
                    Re: Pointer to array of structs?

                    Paminu wrote:

                    ....snip...
                    [color=blue]
                    > Ok I guess that this is correct and identical:
                    >
                    > typedef struct _tnode_t {
                    > void *content;
                    > struct _tnode_t *kids;
                    > } tnode_t;
                    >
                    > int main(void)
                    > {
                    > tnode_t *array;
                    > array[0].kids = NULL;
                    > array->kids = NULL;
                    > return 0;
                    >
                    > }
                    >
                    > when I do this: array[i] I get a struct. When I do this: array->kids I get a
                    > pointer to a struct.[/color]

                    There's still an issue, you've declared a pointer to a struct, but you
                    have not initialized it to a valid value (through a call to
                    malloc/calloc). Your program is dereferencing a pointer that doesn't
                    point to a valid block of memory and so invokes undefined behaviour.

                    Comment

                    • Barry Schwarz

                      #11
                      Re: Pointer to array of structs?

                      On Wed, 08 Feb 2006 09:34:30 +0100, Paminu <sdef@asd.com > wrote:
                      [color=blue]
                      >Robert Gamble wrote:
                      >[color=green]
                      >> Paminu wrote:[color=darkred]
                      >>> Still having a few problems with malloc and pointers.
                      >>>
                      >>> I have made a struct. Now I would like to make a pointer an array with 4
                      >>> pointers to this struct.
                      >>>
                      >>> #include <stdlib.h>
                      >>> #include <stdio.h>
                      >>> typedef struct _tnode_t {
                      >>> void *content;
                      >>> struct _tnode_t *kids;
                      >>> } tnode_t;
                      >>>
                      >>>
                      >>> int main(void)
                      >>> {
                      >>> tnode_t *array;
                      >>> array = malloc(sizeof(t node_t)*4);
                      >>> array[3] = NULL;
                      >>> return 0;
                      >>>
                      >>> }
                      >>>
                      >>> As I understand "array" point to the first element of an array consisting
                      >>> of space for 4 tnode_t structs. But why can't I initialize element at
                      >>> index 3 to NULL?[/color]
                      >>
                      >> tnode_t *array; creates a pointer to a tnode_t structure.[/color]
                      >
                      >
                      >Ok so it will only work if I set array[3] equal to a struct instead of a
                      >pointer to a struct or NULL:
                      >
                      >1)
                      >tnode_t tt;
                      >tt.content = "test";
                      >tt.kids = NULL;
                      >tnode_t *array;
                      >array[0] = tt;[/color]

                      tt is a tnode_t. array is a pointer to a tnode_t. array is not
                      initialized to point anywhere. array[0] would be the first tnode_t it
                      pointed if it did point anywhere. That is why your program is
                      syntactically correct. Some compilers warn you if you attempt to use
                      an uninitialized variable but even without the warning it is still
                      undefined behavior. If you added a call to malloc before using
                      array[0], array would then point to one or more structures and the
                      assignment would be fine.
                      [color=blue]
                      >
                      >
                      >this seems to work and make sense. But if I make:[/color]

                      Seeming to work is just leading down the path of murphy's law.[color=blue]
                      >
                      >2)
                      >int *p;
                      >p = malloc(sizeof(i nt)*4);
                      >p[0] = NULL;
                      >
                      >
                      >this only give the warning: "warning: assignment makes integer from pointer
                      >without a cast".[/color]

                      p is a pointer to int. p[0] is the first uninitialized int it points
                      to. NULL is not a suitable value for int. Hence the warning. Do not
                      think of it as "only". Your program is doing something much different
                      than you expect.
                      [color=blue]
                      >
                      >In 1) if I did: array[0] = NULL; I would get the error:
                      >
                      >"error: incompatible types in assignment".[/color]

                      There is an implementation defined conversion from void* (the type of
                      NULL) to int. There is no such conversion from int* to struct.
                      [color=blue]
                      >
                      >Why the different rules?[/color]

                      Different types.


                      Remove del for email

                      Comment

                      • Roka100@gmail.com

                        #12
                        Re: Pointer to array of structs?

                        Hi,
                        I have to say another problem that do not forget to free() them when
                        you allocated a pointer. Maybe there are no errors ,but that will cause
                        memory leak . ( Maybe your OS is stronger than mine :)

                        Comment

                        • Vladimir S. Oka

                          #13
                          Re: Pointer to array of structs?

                          Roka100@gmail.c om wrote:[color=blue]
                          > Hi,
                          > I have to say another problem that do not forget to free() them when
                          > you allocated a pointer. Maybe there are no errors ,but that will cause
                          > memory leak . ( Maybe your OS is stronger than mine :)[/color]

                          What are you talking about? Who, or what are "them"? Etc...

                          Please quote what and who you're replying to. Read this:

                          "If you want to post a followup via groups.google.c om, don't use
                          the broken "Reply" link at the bottom of the article. Click on
                          "show options" at the top of the article, then click on the
                          "Reply" at the bottom of the article headers." - Keith Thompson
                          More details at: <http://cfaj.freeshell. org/google/>

                          --
                          BR, Vladimir

                          My OS can beat the hell out of yOurS.

                          Comment

                          • Roka100@gmail.com

                            #14
                            Re: Pointer to array of structs?

                            Thanks for mentioning.
                            I am sorry about that, I am new here.
                            And I will pay attention to that.

                            Vladimir S. Oka wrote:[color=blue]
                            > Roka100@gmail.c om wrote:[color=green]
                            > > Hi,
                            > > I have to say another problem that do not forget to free() them when
                            > > you allocated a pointer. Maybe there are no errors ,but that will cause
                            > > memory leak . ( Maybe your OS is stronger than mine :)[/color]
                            >
                            > What are you talking about? Who, or what are "them"? Etc...
                            >
                            > Please quote what and who you're replying to. Read this:
                            >
                            > "If you want to post a followup via groups.google.c om, don't use
                            > the broken "Reply" link at the bottom of the article. Click on
                            > "show options" at the top of the article, then click on the
                            > "Reply" at the bottom of the article headers." - Keith Thompson
                            > More details at: <http://cfaj.freeshell. org/google/>
                            >
                            > --
                            > BR, Vladimir
                            >
                            > My OS can beat the hell out of yOurS.[/color]

                            Comment

                            • Vladimir S. Oka

                              #15
                              Re: Pointer to array of structs?

                              Roka100@gmail.c om wrote:
                              [color=blue]
                              > Thanks for mentioning.
                              > I am sorry about that, I am new here.
                              > And I will pay attention to that.[/color]

                              Good, thank you. Also, don't top-post.

                              --
                              BR, Vladimir

                              43rd Law of Computing:
                              Anything that can go wr
                              fortune: Segmentation violation -- Core dumped

                              Comment

                              Working...