r/C_Programming • u/aartaka • Sep 20 '26
Question A way to warn on NULL and 0 comparisons
Hi y’all. I’m rewriting a small library written by another person, and there are lots of pointer and integer comparisons there. However, these are not caught by the compiler (tried both GCC and Clang,) because they are mostly comparisons between NULL and 0. Comparing NULL to any other integer throws warnings, but not for 0. Is there a way I can force warnings even for such case? Tried these GCC and Clang flags, to no avail:
-Wpointer-arith-Waddress-Wint-to-pointer-cast-Wpointer-to-int-cast-Wincompatible-pointer-types-Wint-conversion
Also tried using C23 nullptr—nope, does not warn.
So, any way I can outlaw NULL==0 comparisons?
Update: it’s -Wzero-as-null-pointer-constant on GCC 15+ or G++, thanks to u/skeeto and u/WittyStick for mentioning it.
26
u/zhivago Sep 20 '26
NULL is defined as an integer expression equal to 0, or such converted to void *.
So (void *)(20 / 2 -5) is valid, as is (23 - 23).
There is a defined conversion from a constant zero void * expression to 0 as an integer.
So, no -- you cannot prevent this comparison in C.
It's baked into the language.
You could try writing a new language which differs on this point, I guess, but it wouldn't be C.
7
u/WittyStick Sep 20 '26
-Wzero-as-null-pointer-constant4
u/skeeto Sep 20 '26 edited Sep 20 '26
GCC 15 has
-Wzero-as-null-pointer-constant, which solves just this problem. If you're stuck with an older GCC, and if the program can parse as C++ then you can use GCC's C++ front end. For example:Then:
For Clang you can use
clang-query, which doesn't require C++:29
u/CounterSilly3999 Sep 20 '26
Null pointer is an architecture dependent feature and not allways internally represented as a value of all bits set to zero. Hence C has a requirement for the integer value 0 to be cast to null pointer at integer conversion to a pointer and back. It is the only way to recognize null pointer -- compare it to integer zero.
11
u/hikilaka Sep 20 '26
Null is defined as 0, is it not?
2
u/qalmakka Sep 21 '26 edited Sep 21 '26
Comparing a pointer with zero is perfectly legal, because while the standard doesn't define what null actually is, it defines that the zero literal converts to it implicitly. i.e. you may have a strange architecture where pointers are structs and null is a non-zero pattern, and still
void *x = 0will end up withxcontaining the value of null.For instance, in C99
#define NULL ((void*)0)is always legal and portable0
u/BarracudaDefiant4702 Sep 20 '26
In C NULL is 0 and is part of the standard. Even if NULL isn't 0 it's defined that the compiler has to treat a comparison to 0 as NULL. Why would you want to outlaw NULL==0 comparisons (NULL==0 is always true)? Perhaps give a code example where you think the compiler should complain (like a pointer to int comparison), but comparing to 0 is explicitly part of the standard.
0
u/questron64 Sep 20 '26
What exactly are you trying to achieve here? As others have pointed out NULL, nullptr and 0 are all more or less the same thing, and a feature of the language is that NULL is equal to 0 for any test of equality and because of that there is no warning, and there doesn't need to be a warning.
This is something a code audit and come typing will fix.
1
u/sciencekm Sep 20 '26
Zero is correct and NULL is just for clarity. Here is what the K&R book says:
0
u/cnbatch Sep 20 '26
How about giving C11’s
_Generica try, together with C23’snullptr:https://godbolt.org/z/415xYax3d
0
u/flyingron Sep 20 '26
Comparing pointers to the integer-pointer-constant (e.g., 0) is perfectly valid.
0
u/TheOtherBorgCube Sep 20 '26
https://c-faq.com/null/varieties.html