# [] intialization of arrays

• 11-25-2002
Saravanan
[] 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
• 11-25-2002
orbitz
First off, A and B are the exact same thing, so why not use A for everything?

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.
• 11-25-2002
Saravanan
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.
• 11-25-2002
Saravanan
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.