RecursiveFree 函数 - 警告:从不兼容的指针类型初始化 [-Wincompatible-pointer-types]
RecursiveFree Function - Warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]
我有一个递归释放的函数:
#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:
struct qnode {
int level;
double xy[2];
struct qnode *child[4];
};
typedef struct qnode Node;
我一直收到错误消息
Warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]
及其指向 'p'。
我不明白为什么会这样。
有人可以解释并告诉我如何解决这个问题吗?
您收到错误消息是因为指向 Node
(child
) 数组的指针无法转换为指向 Node
(p
) 的指针。
由于 child
是指向 Node
的四个指针的数组,因此您必须单独释放它们:
void destroyTree (Node* p)
{
if (!p) return;
for (size_t i = 0; i < 4; ++i)
destroyTree(p->child[i]);
free(p);
}
我有一个递归释放的函数:
#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:
struct qnode {
int level;
double xy[2];
struct qnode *child[4];
};
typedef struct qnode Node;
我一直收到错误消息
Warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]
及其指向 'p'。
我不明白为什么会这样。
有人可以解释并告诉我如何解决这个问题吗?
您收到错误消息是因为指向 Node
(child
) 数组的指针无法转换为指向 Node
(p
) 的指针。
由于 child
是指向 Node
的四个指针的数组,因此您必须单独释放它们:
void destroyTree (Node* p)
{
if (!p) return;
for (size_t i = 0; i < 4; ++i)
destroyTree(p->child[i]);
free(p);
}