简体   繁体   English

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

[英]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 ). 您收到错误消息,因为指向Nodechild )数组的指针不能转换为指向Nodep )的指针。

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.

相关问题 警告:从不兼容的指针类型返回 [-Wincompatible-pointer-types]| - warning: return from incompatible pointer type [-Wincompatible-pointer-types]| C中的函数指针:警告:来自不兼容指针类型[-Wincompatible-pointer-types]的赋值 - Function pointers in C: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types] 来自不兼容指针类型的赋值[Wincompatible-pointer-types] - Assignment from incompatible pointer type[Wincompatible-pointer-types] 来自不兼容的指针类型[-Wincompatible-pointer-types]的赋值 - assignment from incompatible pointer type [-Wincompatible-pointer-types] 警告:从不兼容的指针类型 [-Wincompatible-pointer-types] 传递“接受”的参数 2 - warning: passing argument 2 of ‘accept’ from incompatible pointer type [-Wincompatible-pointer-types] 从不兼容的指针类型 [-Wincompatible-pointer-types] 获取传递“pthread_create”参数 3 的警告 - Getting warning of passing argument 3 of ‘pthread_create’ from incompatible pointer type [-Wincompatible-pointer-types] 警告:从不兼容的指针类型 [-Wincompatible-pointer-types]| 传递 'transform_labels' 的参数 2 | - Warning: passing argument 2 of 'transform_labels' from incompatible pointer type [-Wincompatible-pointer-types]| 警告:C 中不兼容的指针类型 [-Wincompatible-pointer-types] - warning: incompatible pointer types [-Wincompatible-pointer-types] in C 在 c 中将大端转换为小端。 警告:从不兼容的指针类型返回 [-Wincompatible-pointer-types] 返回 src; - converting Big endian to little endian in c. warning:return from incompatible pointer type [-Wincompatible-pointer-types] return src; 从不兼容的指针类型 [-Wincompatible-pointer-types] 传递“send_to_host”的参数 2 - passing argument 2 of ‘send_to_host’ from incompatible pointer type [-Wincompatible-pointer-types]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM