Unable to free const pointers in C

前端 未结 12 1795
梦如初夏
梦如初夏 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:28

    Several people have posted the right answer, but they keep deleting it for some reason. You need to cast it to a non-const pointer; free takes a void*, not a const void*:

    free((char*)str);
    

提交回复
热议问题