第二个选项不能引用自身。例如:
// Works:
struct LinkedListNode_ {
void *value;
struct LinkedListNode_ *next;
};
// Does not work:
typedef struct {
void *value;
LinkedListNode *next;
} LinkedListNode;
// Also Works:
typedef struct LinkedListNode_ {
void *value;
struct LinkedListNode_ *next;
} LinkedListNode;
Foo
是一个命名为 struct myStruct
的 typedef。Foo
是一个未命名的 struct
的 typedef
。Foo
和它所代表的 struct
,而第一个版本则允许。typedef struct myStruct Foo
。 - Aillynstruct node {
int value;
struct node *left;
struct node *right;
};
typedef struct node Tree;
或者
struct A;
struct B;
struct A {
struct B *b;
};
struct B {
struct A *a;
};
typedef struct A AType;
typedef struct B Btype;
typedef struct node {
int value;
struct node *left;
struct node *right;
} Tree;
typedef struct A AType; // You can create a typedef
typedef struct B BType; // for an incomplete type
struct A {
BType *b;
};
struct B {
AType *a;
};