小编pro*_*oom的帖子

如何避免"null参数需要非null null"编译器警告

编译以下代码:

#include <string.h>
#define FOO (NULL)

int main(int argc, char *argv[])
{
    char *foo;

    if (FOO)
        foo = strdup(FOO);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

导致以下编译器警告:

foo.c: In function ‘main’:
foo.c:9:3: warning: null argument where non-null required (argument 1) [-Wnonnull]
   foo = strdup(FOO);
   ^
Run Code Online (Sandbox Code Playgroud)

但是,strdup如果FOONULL因为if (FOO)检查将不会被调用.有没有办法避免这种警告?

谢谢!

c strdup gcc-warning

5
推荐指数
2
解决办法
1万
查看次数

标签 统计

c ×1

gcc-warning ×1

strdup ×1