0

Possible Duplicate:
C : pointer to struct in the struct definition

In my beginer course we need to declare a struct for tree search. I need pointers for reference of same type in each node. This decleration seems incorrect, pnode is not known in this context. How can i archieve my goal?

typedef struct 
{ 
    int value;
    int right;
    int left;
    pnode nextleft;
    pnode nextright;
}node, *pnode;
Community
  • 1
  • 1
Starfighter911
  • 197
  • 2
  • 3
  • 9

2 Answers2

2

the C Faq is a good reference. http://c-faq.com/struct/selfref.html

I tend to use the typedef before the struct method

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;
    };
Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
1
struct node
{
    int value;
    int right;
    int left;
    struct node *nextleft;
    struct node *nextright;
};

Here, node is within the tag namespace.

dreamlax
  • 93,976
  • 29
  • 161
  • 209
  • +1 And if you want to avoid having to use `struct node` and `struct node *` to declare instances of your structure, you can still add `typedef struct node node; typedef struct node* pnode;` after the structure definition. Although for code clarity, I would prefer not to. – JoeFish Dec 21 '11 at 20:24