Search code examples
cstructtypedefconflict

C typedef confliction


So I am attempting to construct a B-Tree, using two types of nodes, the individual key Nodes (kNode) and the super node containing a number of kNodes based on the order size (sibNode). The problem I keep running into is that for this set up to work I need kNode pointers in sibNode (ex. pkey and smallestPnt) as well as sibNode pointers in kNode (ex. nxtkey and child). Whichever typedef I put first however returns the error unknown type for those pointers (example in this order it returns: error: unknown type name 'kNode'). If anyone could give me some advice on how to avoid this error that would be appreciated.

typedef int keyT;

//B-Tree Node typdef
typedef struct
{
int size;
int cursor;
kNode* pkey;
kNode* smallestPnt;

}sibNode;

//key Node typedef
typedef struct 
{

keyT* key;
sibNode* nxtkey;
sibNode* child;

}kNode;

Solution

  • When sibNode type is defined, the type kNode hasn't been defined yet.

    Use forward declaration like this:

    struct kNode;      //this is forward declaration
    
    typedef struct
    {
        int size;
        int cursor;
        struct kNode* pkey;         //here
        struct kNode* smallestPnt;  //here
    }sibNode;
    
    typedef struct kNode       //and here
    {
        keyT* key;
        sibNode* nxtkey;
        sibNode* child;
    }kNode;