I haven't been writing C for very long, and so I'm not sure about how I should go about doing these sorts of recursive things... I would like each cell to contain another cell, but I get an error along the lines of "field 'child' has incomplete type". What's up?
typedef struct Cell {
int isParent;
Cell child;
} Cell;
There is sort of a way around this:
If you declare it like this, it properly tells the compiler that struct Cell and plain-ol'-cell are the same. So you can use Cell just like normal. Still have to use struct Cell inside of the initial declaration itself though.
A Structure which contain a reference to itself. A common occurrence of this in a structure which describes a node for a link list. Each node needs a reference to the next node in the chain.
Another convenient method is to pre-typedef the structure with,structure tag as:
In C, you cannot reference the typedef that you're creating withing the structure itself. You have to use the structure name, as in the following test program:
Although it's probably a lot more complicated than this in the standard, you can think of it as the compiler knowing about
struct Cell
on the first line of thetypedef
but not knowing abouttCell
until the last line :-) That's how I remember that rule.Lets go through basic definition of typedef. typedef use to define an alias to an existing data type either it is user defined or inbuilt.
for example
Confusion here is with the self referential structure, due to a member of same data type which is not define earlier. So In standard way you can write your code as :-
But last option increase some extra lines and words with usually we don't want to do (we are so lazy you know ;) ) . So prefer View 2.
From the theoretical point of view, Languages can only support self-referential structures not self-inclusive structures.