[英]RecursiveFree Function - Warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]
I have a function which recursively frees: 我有一个递归释放的功能:
#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
}
treeStructure.h: treeStructure.h:
struct qnode {
int level;
double xy[2];
struct qnode *child[4];
};
typedef struct qnode Node;
I keep getting the error 我不断收到错误
Warning: initialization from incompatible pointer type [-Wincompatible-pointer-types] 警告:从不兼容的指针类型[-Wincompatible-pointer-types]初始化
and its pointing to 'p'. 并指向“ p”。
I don't understand why this is occurring. 我不明白为什么会这样。
Can someone please explain and inform me how to fix this? 有人可以解释一下并通知我该如何解决吗?
You get the error message because a pointer to an array of Node
( child
) is not convertible to a pointer to Node
( p
). 您收到错误消息,因为指向Node
( child
)数组的指针不能转换为指向Node
( p
)的指针。
As child
is an array of four pointers to Node
you have to free them seperately: 由于child
是指向Node
的四个指针的数组,因此您必须分别释放它们:
void destroyTree (Node* p)
{
if (!p) return;
for (size_t i = 0; i < 4; ++i)
destroyTree(p->child[i]);
free(p);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.