Static initialization of an array of Structs (each of which has anarray member)

M

mike.arsenault

I'm not sure how to explain this problem other that posting the code
that I'm attempting to compile, so here it is:

struct innerStruct
{
int m_RelOID;
int m_ParentClassOID;
int m_InverseRelOID;
int m_InverseParentClassOID;
};

struct outerStruct
{
int m_HashIndex;
struct innerStruct m_InnerArray[];
}
s_OuterStruct[] =
{
0, {
{1, 2, 3, 4},
{5, 6, 7, 8}
},
1, {
{11, 12, 13, 14},
{15, 16, 17, 18},
{25, 26, 27, 28},
}
};

's_OuterStruct' is an array of structs - I'm trying to initialize an
array with 2 elements in it.
The first element has a '0' with an 'inner' array of 2 elements
(of type 'innerStruct').
The second element has a '1' with an 'inner' array of 3 elements
(of type 'innerStruct').

I'm having a hard time coming up with the proper syntax to initialize
this 'array of structs, each of which is composed of a number and
another array of structs'.

Keep in mind that the inner 'array of structs' won't have the same
number of elements, as in the above example.

Is what I'm trying to do even possible with static initialization??

Thanks
Mike
 
A

Andrey Tarasevich

mike.arsenault said:
Is what I'm trying to do even possible with static initialization??

No. C++ language has no such feature. The declaration of your
'outerStruct' is already broken
> struct outerStruct
> {
> int m_HashIndex;
> struct innerStruct m_InnerArray[];
> }
> s_OuterStruct[] =

because in C++ you can't use an array of unspecified size as a class member.

Using a '[]' array as a trailing member of a struct is allowed in C99
version of C language. However, it is intent is to legalize "struct
hack", i.e. support run-time sized arrays as trailing members of a
*dynamically allocated* struct objects. It can't be used with
non-dynamic objects and aggregate initializers, as you are trying to use it.

Finally, having said the above, what you are trying to do is actually
supported as a C language extension in GCC compiler. Nevertheless, it is
not legal C and it is not legal C++.
 
V

Vidar Hasfjord

I'm not sure how to explain this problem other that posting the code
that I'm attempting to compile, so here it is:

struct innerStruct
{
   int      m_RelOID;
   int      m_ParentClassOID;
   int      m_InverseRelOID;
   int      m_InverseParentClassOID;

};

struct outerStruct
{
   int m_HashIndex;
   struct innerStruct m_InnerArray[];}

s_OuterStruct[] =
{
   0, {
      {1, 2, 3, 4},
      {5, 6, 7, 8}
      },
   1, {
      {11, 12, 13, 14},
      {15, 16, 17, 18},
      {25, 26, 27, 28},
      }

};

's_OuterStruct' is an array of structs - I'm trying to initialize an
array with 2 elements in it.
    The first element has a '0' with an 'inner' array of 2 elements
(of type 'innerStruct').
    The second element has a '1' with an 'inner' array of 3 elements
(of type 'innerStruct').

I'm having a hard time coming up with the proper syntax to initialize
this 'array of structs, each of which is composed of a number and
another array of structs'.

Keep in mind that the inner 'array of structs' won't have the same
number of elements, as in the above example.

Is what I'm trying to do even possible with static initialization??

To use static initalization you will have to fix the size of the
member array in outerStruct:

struct outerStruct
{
int m_HashIndex;
enum {MaxArraySize = 3};
innerStruct m_InnerArray [MaxArraySize];
}

Regards,
Vidar Hasfjord
 
S

Saeed Amrollahi

I'm not sure how to explain this problem other that posting the code
that I'm attempting to compile, so here it is:

struct innerStruct
{
   int      m_RelOID;
   int      m_ParentClassOID;
   int      m_InverseRelOID;
   int      m_InverseParentClassOID;

};

struct outerStruct
{
   int m_HashIndex;
   struct innerStruct m_InnerArray[];}

s_OuterStruct[] =
{
   0, {
      {1, 2, 3, 4},
      {5, 6, 7, 8}
      },
   1, {
      {11, 12, 13, 14},
      {15, 16, 17, 18},
      {25, 26, 27, 28},
      }

};

's_OuterStruct' is an array of structs - I'm trying to initialize an
array with 2 elements in it.
    The first element has a '0' with an 'inner' array of 2 elements
(of type 'innerStruct').
    The second element has a '1' with an 'inner' array of 3 elements
(of type 'innerStruct').

I'm having a hard time coming up with the proper syntax to initialize
this 'array of structs, each of which is composed of a number and
another array of structs'.

Keep in mind that the inner 'array of structs' won't have the same
number of elements, as in the above example.

Is what I'm trying to do even possible with static initialization??

Thanks
Mike

Hi Mike
As others mentioned because each object in an array must contain at
least one element
and compiler assumes the size of m_InnerArray is zero, so such error
occured.
As you know there is big diiference between array and vector: the size
of vector can be zero.
Because the C++0x is approaching, may be you like to know, using
inilializer lists
you can write your code much better:
struct outerStruct
{
int m_HashIndex;
vector<innerStruct> v; // empty vector
};

vector<outerStruct> vec = {
{ 0,
{ {1, 2, 3, 4},
{5, 6, 7, 8} }
},
{ 1,
{ {11, 12, 13, 14},
{15, 16, 17, 18},
{25, 26, 27, 28},
}
}
};

As you see, the above code will be more elegant and conform to sprit
of C++.

Regards,
-- Saeed Amrollahi
 
M

mike.arsenault

Thanks for all the answers - There are other ways that I can set up
the structure that I want - it just would've been nice to do it all
with initialization syntax.

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top