从不兼容的指针类型/ deref指针到不完整类型的赋值

时间:2013-11-27 03:49:50

标签: c pointers struct reference dereference

我有这个二叉树,其中每个结构,让我们调用它们A有一个另一个结构类型的指针,让我们调用它们B,指向另一个结构类型B等等(形成结构类型B的链表)。

照片:

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

问题,我不确定。我收到一条错误消息:

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

代码:

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

1 个答案:

答案 0 :(得分:1)

缺少分号:

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

此外,由于您在结构定义中使用了不完整的类型,因此应使用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;
};