C结构,指针
问题描述:
在我beginer当然,我们需要声明的树搜索一个结构。我需要在每个节点中引用相同类型的指针。这种删节看起来不正确,在这种情况下,pnode是不知道的。我怎样才能实现我的目标?
typedef struct
{
int value;
int right;
int left;
pnode nextleft;
pnode nextright;
}node, *pnode;
答
struct node
{
int value;
int right;
int left;
struct node *nextleft;
struct node *nextright;
};
这里,node
是标签命名空间内。
答
C Faq是一个很好的参考。 http://c-faq.com/struct/selfref.html
我倾向于在结构方法
http://c-faq.com/decl/selfrefstruct.html
typedef struct a *APTR;
typedef struct b *BPTR;
struct a {
int afield;
BPTR bpointer;
};
struct b {
int bfield;
APTR apointer;
};
+1之前使用的typedef如果你想避免使用'结构node'和'结构节点*'申报情况你的结构仍然可以添加'typedef struct node node; typedef struct node * pnode;'在结构定义之后。尽管为了代码清晰,我宁愿不要。 – JoeFish 2011-12-21 20:24:29