tags:

views:

252

answers:

5

Hi!

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.

A: 

Yes this is possible.

This is how linked lists are made!

Crowe T. Robot
+12  A: 

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
Thanks a lot! That's the *detail* I didn't know! :)
shazarre
A: 

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
OP said he already did try it.
Grandpa
But, as I wrote, I tried id - didn't work, so I asked here :)
shazarre
+1  A: 

I agree... and trees. You don't have to look at it like "the egg and the chiken" because defining a type always occurs before instantiating one. So having a member variable of the same type of the object is just tricky when you begin to mix the two in your head!

Mike Gleason jr Couturier
+10  A: 

You can use an incomplete type in the typedef:

typedef struct node node;

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