C++ Arrays Problem
Dec 7, 2012 at 7:29pm UTC
So I need to declare and struct with variadic elements like this:
1 2 3 4 5 6 7 8 9 10 11
struct ACTION_TO_TYPE
{
DWORD SomeHeader[8];
struct ACTION_TO_TYPE_IN_STRUCT
{
DWORD m_0;
DWORD m_4;
DWORD m_8;
} //size of 12
m_8[];
} gv = { {0, 0, 0}, {0, 0, 0} };
But this doesn't compile how can I do this. Note I use this structure to initaliaze only global variables with fixed size and then pass it to function which finds the size of the array by the terminating NULL symbol:
1 2 3 4 5 6 7 8 9
func(ACTION_TO_TYPE* a0)
{
ACTION_TO_TYPE_IN_STRUCT * i = a0->m_8;
while (i->m_0)
{
//Some code
i++;
}
}
Dec 7, 2012 at 7:53pm UTC
This is actually called "flexible array member", and it cannot be initialized (also, note that this is C-only, C++ doesn't support this at all)
You could declare it as
m_8[1]
, in which case it is
= { {0, 0, 0}, {{0, 0, 0}} };
fully braced, and it also becomes valid C++ if you need that.
To quote the C standard
After the declaration:
struct s { int n; double d[]; };
the structure struct s has a flexible array member d.
Following the above declaration
1 2
struct s t1 = { 0 }; // valid
struct s t2 = { 1, { 4.2 }}; // invalid
The initialization of t2 is invalid (and violates a constraint) because struct s is treated as if it did not contain member d.
Last edited on Dec 7, 2012 at 7:57pm UTC
Dec 7, 2012 at 10:10pm UTC
Ok but is there someway like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
struct ACTION_TO_TYPE_IN_STRUCT
{
DWORD m_0;
union { DWORD m_dw; BYTE m_by[4]; } m_4;
DWORD m_8;
} ; //size of 12
struct ACTION_TO_TYPE
{
union { DWORD m_dw; BYTE m_by[4]; } m_0;
DWORD m_4;
const ACTION_TO_TYPE_IN_STRUCT* m_8;
} gv = { 0, 0, &{0, 0, 0} } ; //size of
Actually passing a constant pointer. ??
Topic archived. No new replies allowed.