Search code examples
cpointersstructreferencedereference

assignment from incompatible pointer type/deref pointer to incomplete type


I have this binary tree where each struct, lets call them A has a pointer of another struct type, lets call them B, pointing to another struct type B and so forth(forming a linkedlist of struct type B).

Picture:

(A)
 /\
 (A)->(B)->(B)->(B)->||

The problem, i'm not sure. I am receiving an error that says:

AddRemove.c: In function ‘AddRemove’:
AddRemove.c:21: warning: assignment from incompatible pointer type
AddRemove.c:22: error: dereferencing pointer to incomplete type
AddRemove.c:23: error: dereferencing pointer to incomplete type
AddRemove.c:24: error: dereferencing pointer to incomplete type
AddRemove.c:26: error: dereferencing pointer to incomplete type

The code:

struct A{
//other variables
struct A *left,*right;
struct B *queue;         
}*rootT;

struct B{
//other variables
struct B *next;
};

void AddRemove(struct A *aNode, struct B *bNode){
/*aNode is the memory location of the struct node (A) in the picture and bNode is
 a struct node that we want to add to the linkedlist.*/
struct B *Bptr; //Used to go through the linkedlist of struct type B
if(aNode->queue==NULL){ /*If the pointer of node (A) is null then we have the
pointer point to bNode, the node that we wanted to add.*/
    aNode->queue=bNode;
    bNode->next=NULL;
}
else{
    Bptr=aNode->queue; /*Otherwise have the temp pointer point to what
 node (A)'s pointer points to, which should be the first struct type (B)*/
    while(Bptr->next!=NULL){ /*Keep pointing to the next struct of type B until 
we hit the end*/
        Bptr=Bptr->next;
    }
    Bptr->next=bNode;
}
}

Solution

  • Missing semicolon:

    struct B{
        //other variables
        struct B *next;
    };
     ^
    

    Also, since you're using incomplete types inside the structure definitions, you should use typedef:

    typedef struct A A;
    typedef struct B B;
    
    struct A {
        //other variables
        A *left,*right;
        B *queue;         
    } *rootT;
    
    struct B {
        //other variables
        B *next;
    };