-1

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?

anon2000
  • 57
  • 1
  • 8

1 Answers1

1

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);
}
Swordfish
  • 12,971
  • 3
  • 21
  • 43