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
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++.
--
Best regards,
Andrey Tarasevich
> 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[];
> }
C++ wants to have a fixed sizeof for each type. Here, the sizeof
(outerStruct) is fixed after parsing its definition. You cannot enlarge
it later, and you cannot put data in the struct which does not fit there,
neither can you have a type with varying size, and neither can you have
an array with elements having different sizes. So it's not just
initialization what does not work here. You have to use either
std::vector or dynamic memory.
hth
Paavo
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
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
Mike