RecursiveFree函数-警告:从不兼容的指针类型初始化[-Wincompatible指针类型]
我有一个递归释放的函数:RecursiveFree函数-警告:从不兼容的指针类型初始化[-Wincompatible指针类型],c,pointers,recursion,nodes,quadtree,C,Pointers,Recursion,Nodes,Quadtree,我有一个递归释放的函数: #include "treeStructure.h" void destroyTree (Node* p) { if (p==NULL) return; Node* free_next = p -> child; //getting the address of the following item before p is freed free (p); //freeing p destroyTree(free_n
#include "treeStructure.h"
void destroyTree (Node* p)
{
if (p==NULL)
return;
Node* free_next = p -> child; //getting the address of the following item before p is freed
free (p); //freeing p
destroyTree(free_next); //calling clone of the function to recursively free the next item
}
treeststructure.h:
struct qnode {
int level;
double xy[2];
struct qnode *child[4];
};
typedef struct qnode Node;
我一直在犯错误
警告:从不兼容的指针类型初始化[-Wincompatible指针类型]
它指向“p”
我不明白为什么会这样
有人能解释一下并告诉我如何解决这个问题吗?您会收到错误消息,因为指向
节点的数组(子节点
)的指针不能转换为指向节点
(p
)的指针
由于child
是指向Node
的四个指针的数组,因此必须分别释放它们:
void destroyTree (Node* p)
{
if (!p) return;
for (size_t i = 0; i < 4; ++i)
destroyTree(p->child[i]);
free(p);
}
void销毁树(节点*p)
{
如果(!p)返回;
对于(尺寸i=0;i<4;++i)
破坏树(p->child[i]);
自由基(p);
}
@usr我链接了一个具有节点结构的头文件。我在上面添加了它以显示它是如何链接的。Child是指向节点的指针数组,而不是指向节点的单个指针。@swardfish参数应该是Node**p,不是吗?子字段的类型为Node**@anon2000child[i]
的类型为Node*
。