self referential struct definition? Ask Question

self referential struct definition? Ask Question

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;

ベストアンサー1

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;

おすすめ記事