繁体   English   中英

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;

我不断收到错误

警告:从不兼容的指针类型[-Wincompatible-pointer-types]初始化

并指向“ p”。

我不明白为什么会这样。

有人可以解释一下并通知我该如何解决吗?

您收到错误消息,因为指向Nodechild )数组的指针不能转换为指向Nodep )的指针。

由于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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM