8

Is it posible to define a structure with a pointer to that type of structure? 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.

shazarre
  • 225
  • 1
  • 3
  • 9
  • 1
    Possible duplicates: http://stackoverflow.com/questions/506366/ http://stackoverflow.com/questions/588623/ – CB Bailey Nov 28 '09 at 22:58

4 Answers4

25

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.

avakar
  • 32,009
  • 9
  • 68
  • 103
23

You can use an incomplete type in the typedef:

typedef struct node node;

struct node {
  char *name;
  node *parent;
};
sambowry
  • 2,436
  • 1
  • 16
  • 13
0

Yes this is possible.

This is how linked lists are made!

Crowe T. Robot
  • 2,045
  • 17
  • 18
-3

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.

eKek0
  • 23,005
  • 25
  • 91
  • 119