簡體   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