Thread: [] intialization of arrays

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    15

    [] intialization of arrays

    Hello all,
    I have a question about arrays of variable length. This is what I would like to do. I have two structures A and B

    struct A
    {
    int a;
    int b;
    struct A * pSubA;
    };

    struct B
    {
    int c;
    int d;
    struct A * pSubA;
    };

    The idea behind this is that B holds n objects of A and A also can hold n objects of type itself.

    Now how do I statically intialize an object of type B as follows -->

    struct B ob[] =
    {
    {1,1,NULL},
    {1,2,NULL}
    };

    This seems to work. But how do I do the following -->

    struct B ob[] =
    {
    {1,1,{{1,1,NULL},{1,1,NULL},{1,1,NULL}}},
    {1,1,NULL}
    };

    Meaning how do I assign an array of values to the pSubA pointer. The reason for this is that B can hold more than 1 object of type A.

    Also how do I do the following -->

    struct B ob[] =
    {
    {1,1,{{1,1,{{<another A here>}, {<Another A>}}},{1,1,NULL},{1,1,NULL}}},
    {1,1,NULL}
    }

    Appreciate any help.... Thanks in advance

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    First off, A and B are the exact same thing, so why not use A for everything?

    Now for your question:

    Code:
    struct B ob[] =
     {
     {1,1,{{1,1,NULL},{1,1,NULL},{1,1,NULL}}},
     {1,1,NULL}
     };
    You are making a linked list here, what this loks like is:
    Take a structure that takes 2 ints, and then that takes an array of somethign that looks like it. Those {1,1,NULL}'s inside teh {} should not have NULL's, except for the last one.

    Code:
    {1,1,{1,1,{1,1,{1,1,NULL}}}}
    Should be more like what you want.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    15
    Hi orbitz,
    Thanks for that response.
    Actually I am not trying to implement a linked list. There are two seperate structures (A and B are actually a whole lot different) and the idea is that a single instance of A could have N instances of B in it. Also a single instance of B could have N instances of B associated with it. So that would mean that I can have something like this

    A1
    B1
    B11
    B111
    B112
    B12
    B2
    B3

    The linked list concept would not work for this model. I will need to declare it as an array. How is that possible? Thank you once again for your reply.

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    15
    Hi folks,
    Thank you for the reply, Salem. I have a compile problem now pertaining to the same issue. Why cant I do the following

    Code:
    struct A
    {
        int a;
        A * ptr[];
    };
    
    A ob[] =
    {
         {1, {&ob[1],&ob[2],&ob[3]}}
    };
    it gives me an excess elements in aggregate intializer error message. This however goes away if I use A*ptr[5] instead of a variable length array. How do I solve this? I dont want to use the [5] because that would mean I will waste space for empty elements.

    Thanks in advance

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  2. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  3. malloc with arrays of strings
    By Lib in forum C Programming
    Replies: 2
    Last Post: 08-03-2003, 10:46 PM
  4. Arrays Indexing Intialization
    By jshamilton73 in forum C++ Programming
    Replies: 7
    Last Post: 06-06-2003, 02:26 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM