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;
0

2 Answers 2

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;
    };
Sign up to request clarification or add additional context in comments.

Comments

1
struct node
{
    int value;
    int right;
    int left;
    struct node *nextleft;
    struct node *nextright;
};

Here, node is within the tag namespace.

1 Comment

+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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.