0

我有一个非常简单的问题:我想在另一个结构中使用结构,但我希望能够以我想要的任何顺序定义它们。像这样的东西:

// 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;
};

我怎样才能做到这一点?

4

1 回答 1

1

实现这一点的唯一方法是使用指向结构的指针而不是静态成员:

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;

这样做的原因是编译器必须知道结构体的大小。如果您使用指针,编译器只需要知道该结构类型只是存在,因为给定架构上的指针类型在编译时是已知大小

于 2012-12-05T16:51:32.450 回答