self referential struct definition?

Ziggy picture Ziggy · Feb 26, 2009 · Viewed 112.6k times · Source

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;

Answer

Andrew Grant picture Andrew Grant · Feb 26, 2009

Clearly a Cell cannot contain another cell as it becomes a never-ending recursion.

However a Cell CAN contain a pointer to another cell.

typedef struct Cell {
  bool isParent;
  struct Cell* child;
} Cell;