开发者

C structure with pointer to self [duplicate]

This question already has answers here: C: pointer to struct in the struct definition (6 answers) Closed 8 years ago.

Is it posible to define a structure with a pointer to that type of 开发者_如何学JAVAstructure? What I mean is:

typedef struct {
    char* name;
    node* parent;
} node;

As far as I tried or read, I don't know how to do this or if it's even possible.


Yes, but you have to name the structure, so that you can refer to it.

typedef struct node_ {
    char* name;
    struct node_ * parent;
} node;

The name node only becomes declared after the structure is fully defined.


You can use an incomplete type in the typedef:

typedef struct node node;

struct node {
  char *name;
  node *parent;
};


Yes this is possible.

This is how linked lists are made!


Why don't you try it? You have to put a name to the structure, and yes, this is the way in that recursive data structures works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜