The standard says:
"An integer constant expression with the value 0, or such an expression cast to type
void*
, is called a null pointer constant.67) If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.""67) The macro NULL is defined in stddef.h (and other headers) as a null pointer constant; see 7.19."
Source: ISO/IEC 9899:2018 (C18), §6.2.3.2/3 "Pointers".
The most common null pointer constants are of course, 0
and (void*) 0
used by most implementations as null pointer constant, but as the standard mandates - "An integer constant expression with the value 0, or such an expression cast to type void*
" - a null pointer constant shall also be any of the following:
1 * 0
0 * 0
0 - 0
25 - 25
(-4) + (4)
(0 * ((0 * 25) * 3)
(0) * (-100)
Like any of their pendants preceded by (void*)
, f.e. (void*) (1 * 0)
or (void*) (25 - 25)
.
As well as boolean expressions:
(void*) ((1 + 1) == 25)
(void*) !(9)
Thus, any statement like one of these:
int* ptr = 25 - 25;
int* ptr = (void*) ((-4) + 4);
int* ptr = (0 * ((0 * 25) * 3);
int* ptr = (void*) !(9);
int* ptr = ((1 + 1) == 25);
shall make ptr
, per standard, a null pointer.
- Am I correct or is there anything wrong about my concerns?
I am looking for any part of the C standard which invalidates this thesis.
As far as I searched, there shouldn´t be a duplicate of this question on Stack Overflow.
25 - 25
is a constant. – Eugene Sh.(void*) strlen("")
would not be guaranteed to be a null pointer. – jamesdlin