Unable to free const pointers in C

前端 未结 12 1810
梦如初夏
梦如初夏 2020-11-28 06:56

How can I free a const char*? I allocated new memory using malloc, and when I\'m trying to free it I always receive the error \"incompatible pointe

12条回答
  •  忘掉有多难
    2020-11-28 07:15

    I could be wrong but I think the problem lies in const. Cast the pointer to non-const like:

    free((char *) p);
    

    Because with const you say: Don't change the data this pointer points to.

提交回复
热议问题