Free() pointer error while casting from const char*

て烟熏妆下的殇ゞ 提交于 2020-04-14 06:19:20

问题


I am trying to free up char* acc at the end of this code:

 while (fgets(line, 300, fp)!=NULL)
        {
                char *tmp = strdup(line);

                char *acc = (char*) getfield(tmp, 2);

                //check if account number already exists
                if (acc != NULL){
                       if ((atoi(acc))== accNum){

                       return 1;
                       }
                }

                free(tmp);
                free(acc);
        }

getfield returns a const char* so I have casted it to char*, but an error arises from the pointer when I try to free() saying free(): invalid pointer. What am I doing wrong? Any help would be appreciated

来源:https://stackoverflow.com/questions/60685212/free-pointer-error-while-casting-from-const-char

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!