GCC gives me an "array type has incomplete element type"-error message when i try to compile this:
typedef struct _node node;
struct _node{
int foo;
node (*children)[2];
int bar;
};
In memory the struct should look like this
0x345345000000 foo
0x345345000004 pointer to 1. child node
0x345345000008 pointer to 2. child node
0x34534500000C bar
Instead of this:
Use this:
That's because the C type expression works the other way round.
This:
reads as the following: the contents of
children
, when dereferenced (*
is applied to them), yield something on which the array operator[]
can be applied, yielding anode
. Which means thatchildren
is a pointer to an array of twonode
. However, you do not want a pointer to an array, you want an array of two pointers. For that, you should put the parentheses thus:which then reads as: the contents of
children
are an array of two values; applying*
on one of them yields anode
. This is what you want: an array of two pointers tonode
. Note that this is equivalent to:because in the C syntax, when it comes to type expressions, the
[]
operator takes precedence over the*
operator.In your initial version, the C compiler grumbles because at the time it reads the declaration, it does not know what size
node
is (the structure definition is not complete yet), and, as such, has trouble imagining a type "array of twonode
". This is a rather indirect symptom of the problem.Side note: you should refrain from using
_node
or any other identifier beginning with an underscore in your code. These identifiers are reserved for "the implementation". In practice, system headers may use them, and collisions could prove harmful. Personally, I tend to add the underscore at the end, like this:typedef struct node_ node;