My code is something like this:
#define ZERO_CONSTANT 0
foo(size_t * p_size);
foo(ZERO_CONSTANT); //Doesn't warn
Which gcc flag will make the call foo(ZERO_CONSTANT)
to warn?
My code is something like this:
#define ZERO_CONSTANT 0
foo(size_t * p_size);
foo(ZERO_CONSTANT); //Doesn't warn
Which gcc flag will make the call foo(ZERO_CONSTANT)
to warn?
No compilation flag can possibly help you here. The C standard defines the literal 0 to stand in for the null pointer value of any type. Quite often you'll see 0
, NULL
and (void*)0
.
(Note well that this does not imply that the memory address is 0. Because it's undefined behaviour in general to assign an integral value to any pointer type, 0 is used as a placeholder literal for the null pointer).
There is a new option (available since [probably] GCC 4.7.x) - -Wzero-as-null-pointer-constant. It does what you expect, but is valid only for C++... In C the code you presented is perfectly legal, as NULL is usually just "0".
As has been mentioned, it's perfectly valid to pass 0
as a pointer. You can generate a warning with some compilers though if you are prepared to change the definition for ZERO_CONSTANT
:
#define ZERO_CONSTANT ((int)0)
This then generates a warning with clang:
warning: expression which evaluates to zero treated as a null pointer constant of type 'size_t *' (aka 'unsigned long *') [-Wnon-literal-null-conversion]
expanded from macro 'ZERO_CONSTANT'
#define ZERO_CONSTANT ((int)0)
Unfortunately it looks like gcc (4.8) still does not generate a warning in this case, even with -Wextra
, although it's possible that there is a further -W
switch that might enable this.