6

Do I need to use typedef in order to build recursive structs? I tried to use the following code without success:

struct teste
{
    int data;
    int data2;
    struct teste to_teste;
};
Coding Mash
  • 3,338
  • 5
  • 24
  • 45
user1843665
  • 353
  • 2
  • 3
  • 7

2 Answers2

14

To build recursive structs you do not need typedef.

You will have to convert the struct object into a struct pointer object.

like this:

struct teste{
  int data;
  int data2;
  struct teste *to_teste;
};
Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
6

You CANNOT have the same structure inside itself. If you do that then size of that structure becomes indefinite. So that is not allowed.

Instead you can have a pointer to the same structure inside itself to solve your purpose. This will work because size of a pointer is known to the compiler and the structure has a definite size now.

CCoder
  • 2,305
  • 19
  • 41