By Ziggy

2009-02-26 00:47:05 8 Comments

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;


@paxdiablo 2009-02-26 01:27:11

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:

#include <stdio.h>
#include <stdlib.h>

typedef struct Cell {
  int cellSeq;
  struct Cell* next; /* 'tCell *next' will not work here */
} tCell;

int main(void) {
    int i;
    tCell *curr;
    tCell *first;
    tCell *last;

    /* Construct linked list, 100 down to 80. */

    first = malloc (sizeof (tCell));
    last = first;
    first->cellSeq = 100;
    first->next = NULL;
    for (i = 0; i < 20; i++) {
        curr = malloc (sizeof (tCell));
        curr->cellSeq = last->cellSeq - 1;
        curr->next = NULL;
        last->next = curr;
        last = curr;

    /* Walk the list, printing sequence numbers. */

    curr = first;
    while (curr != NULL) {
        printf ("Sequence = %d\n", curr->cellSeq);
        curr = curr->next;

    return 0;

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 the typedef but not knowing about tCell until the last line :-) That's how I remember that rule.

@rimalonfire 2018-05-10 13:09:15

what about c++ can you please link the answers regarding c++

@paxdiablo 2018-05-10 13:12:20

@rimiro, the question was a C one. If you want the answer for a C++ variant, you should ask it as a question.

@m.eldehairy 2016-12-11 17:28:27

All previous answers are great , i just thought to give an insight on why a structure can't contain an instance of its own type (not a reference).

its very important to note that structures are 'value' types i.e they contain the actual value, so when you declare a structure the compiler has to decide how much memory to allocate to an instance of it, so it goes through all its members and adds up their memory to figure out the over all memory of the struct, but if the compiler found an instance of the same struct inside then this is a paradox (i.e in order to know how much memory struct A takes you have to decide how much memory struct A takes !).

But reference types are different, if a struct 'A' contains a 'reference' to an instance of its own type, although we don't know yet how much memory is allocated to it, we know how much memory is allocated to a memory address (i.e the reference).


@Keynes 2016-05-24 18:43:17

Another convenient method is to pre-typedef the structure with,structure tag as:

//declare new type 'Node', as same as struct tag
typedef struct Node Node;
//struct with structure tag 'Node'
struct Node
int data;
//pointer to structure with custom type as same as struct tag
Node *nextNode;
//another pointer of custom type 'Node', same as struct tag
Node *node;

@vineetv2821993 2014-12-23 10:19:30

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.

typedef <data_type> <alias>;

for example

typedef int scores;

scores team1 = 99;

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 :-

//View 1
typedef struct{ bool isParent; struct Cell* child;} Cell;

//View 2
typedef struct{
  bool isParent;
  struct Cell* child;
} Cell;

//Other Available ways, define stucture and create typedef
struct Cell {
  bool isParent;
  struct Cell* child;

typedef struct Cell Cell;

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.

@melpomene 2018-02-28 01:38:02

Your explanation of the typedef syntax is incorrect (consider e.g. typedef int (*foo)(void);). Your View 1 and View 2 examples don't work: They make struct Cell an incomplete type, so you can't actually use child in your code.

@Shawn 2012-03-24 01:47:42

I know this post is old, however, to get the effect you are looking for, you may want to try the following:


/* Forward declaration of "struct Cell" as type Cell. */
typedef struct Cell Cell;

   Define Cell structure taking advantage of forward declaration.
struct Cell
   int isParent;
   Cell *child;


/* could define it as other posters have mentioned without taking
   advantage of the forward declaration.
struct Cell
   int isParent;
   struct Cell *child;


    Some code here...

/* Use the Cell type. */
Cell newCell;

In either of the two cases mentioned in the code fragment above, you MUST declare your child Cell structure as a pointer. If you do not, then you will get the "field 'child' has incomplete type" error. The reason is that "struct Cell" must be defined in order for the compiler to know how much space to allocate when it is used.

If you attempt to use "struct Cell" inside the definition of "struct Cell", then the compiler cannot yet know how much space "struct Cell" is supposed to take. However, the compiler already knows how much space a pointer takes, and (with the forward declaration) it knows that "Cell" is a type of "struct Cell" (although it doesn't yet know how big a "struct Cell" is). So, the compiler can define a "Cell *" within the struct that is being defined.

@DARSHINI DESAI 2010-10-24 13:10:23

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.

struct node
       int data;
       struct node *next; // <-self reference

@Sundar 2009-07-29 19:13:40

From the theoretical point of view, Languages can only support self-referential structures not self-inclusive structures.

@Marsh Ray 2009-07-29 19:23:32

From the practical point of view, how big would such an instance of 'struct Cell' actually be?

@TonyK 2010-09-23 20:28:03

On most machines, four bytes bigger than itself.

@Benjamin Horstman 2009-07-29 19:05:30

There is sort of a way around this:

struct Cell {
  bool isParent;
  struct Cell* child;

struct Cell;
typedef struct Cell Cell;

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.

@MAKZ 2015-03-08 03:43:06

why did you write struct Cell; again ?

@Tyler Crompton 2015-10-02 22:36:57

@MAKZ because the typedef hasn't been executed by the compiler at the time that it's compiling the definition of struct Cell.

@yyny 2016-03-07 19:06:23

@TylerCrompton if the above code block is put into a single C source file, then the typedef has been "executed by the compiler", making the extra struct Cell; redundant. If, however, for some reason you put the last two lines into a header file which you include before you define the Cell struct with the first four lines, then the extra struct Cell; is nececairy.

@Tomáš Zato - Reinstate Monica 2016-11-26 23:03:08

This doesn't even compile under C99 standard.

@melpomene 2018-02-28 01:40:40

@YoYoYonnY No, you can still just write typedef struct Cell Cell; and it will make Cell an alias for struct Cell. It doesn't matter whether the compiler has seen struct Cell { .... } before.

@Jonathan Leffler 2019-11-14 06:24:22

@TomášZato — yes, it does compile under C90, C99, C11, C18.

@Andrew Grant 2009-02-26 00:52:27

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;

@fredoverflow 2015-12-30 13:28:44

@cs01 No, Cell is not in scope yet.

@noɥʇʎԀʎzɐɹƆ 2016-06-06 01:06:30

It would make sense. Python allows it and even allows serialization of such an object. Why not C++?

@Tomáš Zato - Reinstate Monica 2016-11-26 23:02:43

I get warnings when I try to assign Cell* to cell->child.

@jazzpi 2017-02-03 15:31:02

@noɥʇʎԀʎzɐɹƆ Because Python abstracts away pointers so that you don't notice them. Since structs in C basically just store all of their values next to each other, it would be impossible to actually store a struct in itself (because that struct would have to contain another one and so on, leading to a memory structure of infinite size).

@wizzwizz4 2017-05-27 13:53:58

For an explanation of the use of struct Cell, see this answer.

@ikegami 2019-06-29 06:55:44

Re "Python allows it and even allows serialization of such an object.", The fact that a structure can't include itself is universal as it would create an infinitely large structure. Python may not use the * syntax for pointers/references, but it still uses a pointer/reference as required here.

@Jonathan Leffler 2019-11-14 06:33:33

Note C11 § Structure and union specifiers ¶3: A structure or union shall not contain a member with incomplete or function type (hence, a structure shall not contain an instance of itself, but may contain a pointer to an instance of itself), …

Related Questions

Sponsored Content

12 Answered Questions

[SOLVED] Why isn't sizeof for a struct equal to the sum of sizeof of each member?

28 Answered Questions

[SOLVED] When should I use a struct rather than a class in C#?

  • 2009-02-06 17:37:55
  • Alex Baranosky
  • 274680 View
  • 1401 Score
  • 28 Answer
  • Tags:   c# struct

8 Answered Questions

[SOLVED] Difference between 'struct' and 'typedef struct' in C++?

  • 2009-03-04 20:41:12
  • criddell
  • 524094 View
  • 839 Score
  • 8 Answer
  • Tags:   c++ struct typedef

15 Answered Questions

[SOLVED] Why should we typedef a struct so often in C?

  • 2008-10-31 07:14:03
  • Manoj Doubts
  • 522211 View
  • 410 Score
  • 15 Answer
  • Tags:   c struct typedef

23 Answered Questions

25 Answered Questions

[SOLVED] When should you use a class vs a struct in C++?

  • 2008-09-10 16:29:54
  • Alan Hinchcliffe
  • 406880 View
  • 968 Score
  • 25 Answer
  • Tags:   c++ oop class struct ooad

2 Answered Questions

[SOLVED] typedef struct in header file struct definition in c file

2 Answered Questions

[SOLVED] Issue initialising values for typedef field in struct - C

  • 2013-08-05 21:07:47
  • Racktash
  • 95 View
  • 0 Score
  • 2 Answer
  • Tags:   c struct typedef

2 Answered Questions

Sponsored Content