Avoiding the warning about "null argument where non-zero"
Compiling the following code:
#include <string.h>
#define FOO (NULL)
int main(int argc, char *argv[])
{
char *foo;
if (FOO)
foo = strdup(FOO);
return 0;
}
leads to the following compiler warning:
foo.c: In function βmainβ:
foo.c:9:3: warning: null argument where non-null required (argument 1) [-Wnonnull]
foo = strdup(FOO);
^
However, strdupit will not be called if FOOthere is NULLdue to verification if (FOO). Is there any way to avoid this warning?
Thank!
, strdup , , strdup NULL.
, , - , , .
NULL , , NULL.
.
if (FOO) foo = strdup(FOO?FOO:"");
if (FOO) foo = strdup(FOO + !FOO);
"" ( , ), strdup NULL, if , , NULL.
, , , :
#define NON_NULL(x) ((x)?(x):"")
, :
#define NON_NULL(x) ((x)?(x):(abort(),""))
GNU ?: ( ), (x) .
#define NON_NULL(x) ((x)?:"")
, :
#define NON_NULL(x) ((x)?:(abort(),"")
- , , -, :
if (FOO) foo = strdup(NON_NULL(FOO));
, NON_NULL - .