简体   繁体   中英

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:

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]

and its pointing to '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 ).

As child is an array of four pointers to Node you have to free them seperately:

void destroyTree (Node* p)
{
    if (!p) return;

    for (size_t i = 0; i < 4; ++i)
        destroyTree(p->child[i]);

    free(p);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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