Initializing array of structure!!

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

    Initializing array of structure!!

    All,

    I need to initialize an array of structures but I don't know how may
    elements are there. I tried to malloc the array but I am not sure how
    to initialize them.

    Snippet:
    struct myarray{
    int conf;
    char * var;
    };

    int main(int args, char * argv[]){
    struct myarray *MYARRAY[]= malloc(sizeof(s truct myarray) *
    30);
    MYARRAY={{1,"x" }, {2,"y"} ...... }; // How do I initialize
    this?
    }

    This comes out with segmentation fault. How do I do this?

    - Shekar
  • Neo

    #2
    Re: Initializing array of structure!!


    "Chandrashe kar Tippur" <ctippur@msn.co m> wrote in message
    news:629c1390.0 411182225.4e65c 0db@posting.goo gle.com...[color=blue]
    > All,
    >
    > I need to initialize an array of structures but I don't know how may
    > elements are there. I tried to malloc the array but I am not sure how
    > to initialize them.
    >
    > Snippet:
    > struct myarray{
    > int conf;
    > char * var;
    > };
    >
    > int main(int args, char * argv[]){
    > struct myarray *MYARRAY[]= malloc(sizeof(s truct myarray) *
    > 30);
    > MYARRAY={{1,"x" }, {2,"y"} ...... }; // How do I initialize
    > this?
    > }
    >
    > This comes out with segmentation fault. How do I do this?
    >
    > - Shekar[/color]

    You can do it like this :

    #include<stdio. h>
    #include<stdlib .h>

    #define ELEMENT_COUNT 4

    struct mystruct {
    int i;
    char *p;
    };

    int main()
    {
    int i;
    struct mystruct *MYARRAY = NULL;
    if(MYARRAY = malloc(sizeof(s truct mystruct) * ELEMENT_COUNT))
    {
    for(i=0; i < ELEMENT_COUNT; i++)
    {
    MYARRAY[i].i = i;
    MYARRAY[i].p = NULL;
    }
    for(i=0; i < ELEMENT_COUNT; i++)
    printf("%d : %p\t", MYARRAY[i].i, MYARRAY[i].p);

    free(MYARRAY);
    }
    return 0;
    }

    -Neo


    Comment

    • Method Man

      #3
      Re: Initializing array of structure!!


      "Chandrashe kar Tippur" <ctippur@msn.co m> wrote in message
      news:629c1390.0 411182225.4e65c 0db@posting.goo gle.com...[color=blue]
      > All,
      >
      > I need to initialize an array of structures but I don't know how may
      > elements are there.[/color]

      In your posted code, you seem to know the element count (30).
      [color=blue]
      > I tried to malloc the array but I am not sure how
      > to initialize them.[/color]

      You're initialization technique works, but you've incorrectly declared your
      array. See below.
      Also, make sure to include this:

      #include<stdio. h>
      [color=blue]
      >
      > Snippet:
      > struct myarray{
      > int conf;
      > char * var;
      > };
      >
      > int main(int args, char * argv[]){
      > struct myarray *MYARRAY[]= malloc(sizeof(s truct myarray) *
      > 30);[/color]

      Change that to:
      struct myarray *MYARRAY = malloc(sizeof(M YARRAY) * 30);

      [snip]


      Comment

      • Method Man

        #4
        Re: Initializing array of structure!!


        "Method Man" <a@b.c> wrote in message
        news:PXind.1228 6$14.391@read1. cgocable.net...[color=blue]
        >
        > "Chandrashe kar Tippur" <ctippur@msn.co m> wrote in message
        > news:629c1390.0 411182225.4e65c 0db@posting.goo gle.com...[color=green]
        > > All,
        > >
        > > I need to initialize an array of structures but I don't know how may
        > > elements are there.[/color]
        >
        > In your posted code, you seem to know the element count (30).
        >[color=green]
        > > I tried to malloc the array but I am not sure how
        > > to initialize them.[/color]
        >
        > You're initialization technique works,[/color]

        Oops, no it doesn't.


        Comment

        • Method Man

          #5
          Re: Initializing array of structure!!


          "Method Man" <a@b.c> wrote in message
          news:PXind.1228 6$14.391@read1. cgocable.net...[color=blue]
          >
          > "Chandrashe kar Tippur" <ctippur@msn.co m> wrote in message
          > news:629c1390.0 411182225.4e65c 0db@posting.goo gle.com...[color=green]
          > > All,
          > >
          > > I need to initialize an array of structures but I don't know how may
          > > elements are there.[/color]
          >
          > In your posted code, you seem to know the element count (30).
          >[color=green]
          > > I tried to malloc the array but I am not sure how
          > > to initialize them.[/color]
          >
          > You're initialization technique works,[/color]

          Oops, no it doesn't.


          Comment

          • Peter Smithson

            #6
            Re: Initializing array of structure!!

            In article <629c1390.04111 82225.4e65c0db@ posting.google. com>,
            ctippur@msn.com says...[color=blue]
            > All,
            >
            > I need to initialize an array of structures but I don't know how may
            > elements are there. I tried to malloc the array but I am not sure how
            > to initialize them.[/color]

            How about -

            struct myarray{
            int conf;
            char * var;
            };

            int main(int args, char * argv[])
            {
            struct myarray MYARRAY[]= {{1,"x"}, {2,"y"}};
            /* Check that it worked - */
            printf("%d %s\n", MYARRAY[0].conf, MYARRAY[0].var);
            printf("%d %s\n", MYARRAY[1].conf, MYARRAY[1].var);
            }


            --

            Comment

            • Mark McIntyre

              #7
              Re: Initializing array of structure!!

              On 18 Nov 2004 22:25:46 -0800, in comp.lang.c , ctippur@msn.com
              (Chandrashekar Tippur) wrote:
              [color=blue]
              >All,
              >
              >I need to initialize an array of structures but I don't know how may
              >elements are there. I tried to malloc the array but I am not sure how
              >to initialize them.
              >
              >Snippet:
              >struct myarray{
              > int conf;
              > char * var;
              >};
              >
              >int main(int args, char * argv[]){
              > struct myarray *MYARRAY[]= malloc(sizeof(s truct myarray) * 30);[/color]

              MYARRAY is an array of pointers to struct myarray. You don't want that.
              You want an array of structs.... so remove the square brackets.

              Also, by convention ALL CAPS is reserved for macro names. Its bad practice
              to use it for variables as it will confuse code maintainers later.

              [color=blue]
              > MYARRAY={{1,"x" }, {2,"y"} ...... }; // How do I initialize this?[/color]

              If its dynamically created, you must dynamically assign values to it by
              looping. Since you know how large it is, you could however have done

              struct myarray TheArray[30] = {{1,"x"},{2,"y" }};
              which would have populated the 1st to elements of the array, and set the
              rest to zero or null.


              --
              Mark McIntyre
              CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
              CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

              ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
              http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
              ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

              Comment

              • Jack Klein

                #8
                Re: Initializing array of structure!!

                On Fri, 19 Nov 2004 04:23:04 -0500, "Method Man" <a@b.c> wrote in
                comp.lang.c:
                [color=blue]
                >
                > "Chandrashe kar Tippur" <ctippur@msn.co m> wrote in message
                > news:629c1390.0 411182225.4e65c 0db@posting.goo gle.com...[color=green]
                > > All,
                > >
                > > I need to initialize an array of structures but I don't know how may
                > > elements are there.[/color]
                >
                > In your posted code, you seem to know the element count (30).
                >[color=green]
                > > I tried to malloc the array but I am not sure how
                > > to initialize them.[/color]
                >
                > You're initialization technique works, but you've incorrectly declared your
                > array. See below.
                > Also, make sure to include this:
                >
                > #include<stdio. h>[/color]
                #include <stdlib.h>
                [color=blue]
                >[color=green]
                > >
                > > Snippet:
                > > struct myarray{
                > > int conf;
                > > char * var;
                > > };
                > >
                > > int main(int args, char * argv[]){
                > > struct myarray *MYARRAY[]= malloc(sizeof(s truct myarray) *
                > > 30);[/color]
                >
                > Change that to:
                > struct myarray *MYARRAY = malloc(sizeof(M YARRAY) * 30);[/color]

                No, no, NO!!!

                struct myarray *MYARRAY = malloc(sizeof *MYARRAY * 30);

                The OP wants an array of 30 structures, not 30 pointers to struct.

                --
                Jack Klein
                Home: http://JK-Technology.Com
                FAQs for
                comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
                comp.lang.c++ http://www.parashift.com/c++-faq-lite/
                alt.comp.lang.l earn.c-c++

                Comment

                Working...