【发布时间】:2013-12-12 13:36:41
【问题描述】:
我有这个二叉树,其中每个结构,我们称它们为 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;
}
}
【问题讨论】:
-
在定义
struct A之前添加声明:struct B; -
这并没有改变我收到的错误。我应该提到这些结构声明在一个单独的 .h 文件中。
-
您是否在此源代码中包含标头?如果没有,您需要这样做。如果您不将它们包含在翻译单元中,编译器将无法预测类型。
-
是的,我把它包括在内,这不是问题。
标签: c pointers struct reference dereference