gpt4 book ai didi

c - 从不兼容指针类型/解引用指针到不完整类型的赋值

转载 作者:行者123 更新时间:2023-11-30 19:21:19 24 4
gpt4 key购买 nike

我有一个二叉树,其中每个结构(我们称它们为 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 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;
};

关于c - 从不兼容指针类型/解引用指针到不完整类型的赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20233528/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com