Replace function or define by the text itself [duplicate]

牧云@^-^@ 提交于 2019-12-12 20:24:43

问题


How can I replace all occurence of

check_free_set_to_null(&pointer)

by

if (pointer)
{
    free(pointer)
    printf(pointer have been freed)
}
else
{
    printf(pointer couldnt had been freed)
    return (1)
}

maybe using some sort of

{1} = check_free_set_to_null(CONTENT)

  if ({1})
    {
        free({1})
        printf({1} have been freed)
    }
    else
    {
        printf({1} couldnt had been freed)
        return (1)
    }

how could I do that ? (take note thats its not exactly what that have to be replaced, its just an example) Im not looking for the compiler to interpret it as multiple line, im looking for modifying the file.


回答1:


You can use macros for this job.

#define check_free_set_to_null(pointer) if (pointer) { \
    free(pointer)\
    printf(pointer have been freed)\
} else {\
    printf(pointer couldnt had been freed)\
    return (1)\
}


来源:https://stackoverflow.com/questions/44869388/replace-function-or-define-by-the-text-itself

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