0

I have a very simple question: I want to use structs inside another structs but I want to be able to define them in any order I want. Something like this:

// User type definition
typedef struct type1{
    int i;
    type2 t;
};
// User type definition
typedef struct type2{
    int i;
    type3 t;
};
// User type definition
typedef struct type3{
    int i;
};

How can I do this?

ljedrz
  • 20,316
  • 4
  • 69
  • 97
Tommaso DS
  • 211
  • 2
  • 14
  • There must be a partial order between the different structs and you should respect that in the order in which you declare them – igon Dec 05 '12 at 16:48
  • 1
    Note: Your typedefs lack a typedef name, it should be `typedef struct type2 { int i; type3 t; } type2;` for example. – Daniel Fischer Dec 05 '12 at 16:59
  • See also http://stackoverflow.com/a/8156472/1407067 – ellotheth Dec 05 '12 at 17:05
  • possible duplicate of [typedef stuct with forward declaration in C](http://stackoverflow.com/questions/8156438/typedef-stuct-with-forward-declaration-in-c) – Mario S Dec 05 '12 at 23:13

1 Answers1

1

The only way this can be accomplished is by using pointers to the structs instead of static members:

typedef struct type1 {
    int i;
    struct type2 *t;
} type1;
// User type definition
typedef struct type2 {
    int i;
    struct type3 *t;
} type2;
// User type definition
typedef struct type3 {
    int i;
} type3;

The reason for this is the compiler MUST know how large the struct is as it gets to it. If you use pointers, all the compiler needs to know is that that struct type simply exists, since pointer types on a given architecture are a known size at compile time

Nikos C.
  • 50,738
  • 9
  • 71
  • 96
Dan F
  • 17,654
  • 5
  • 72
  • 110
  • Neither with use of header file? – Tommaso DS Dec 05 '12 at 16:54
  • @TommasoDeSica I'm sorry, I don't think I understand what you're asking. My method can be accomplished using forward declarations. This is a simple way of telling the compiler that "This struct will be defined later, I promise. I just need to let you know it exists" – Dan F Dec 05 '12 at 16:57
  • This solution is only for C or also for C++? I've tryed it on C++ compiler (now I have only that) and I receive an error. – Tommaso DS Dec 05 '12 at 17:03
  • @NikosC. Thanks, it still needs forward declarations for it to work – Dan F Dec 05 '12 at 18:20
  • They're already there, inlined with the actual declaration. For example: `struct type2 *t;` This is forward declaring `type2` and creating `t` at the same time. – Nikos C. Dec 05 '12 at 18:21
  • @NikosC. I was unaware you could do forward declares in C/C++ like that – Dan F Dec 05 '12 at 18:23
  • @NikosC If I use this solution, I've got some problems with future assignments. – Tommaso DS Dec 06 '12 at 08:18
  • @TommasoDS Yes, you will need to work with pointers, and that usually means allocating the structs dynamically with malloc(). So it's either that, or change the order of your structs. Since you said you don't want to change the order, you will need to work with pointers. – Nikos C. Dec 06 '12 at 12:43