This is an old story: it goes back to C.
There is no null keyword in C. null pointer constant in C:
- integral constant expression with value 0, for example
0 , 0L , '\0' (remember that char is an integral type), (2-4/2) - an expression other than
void* , for example (void*)0 , (void*)0L , (void*)'\0' , (void*)(2-4/2)
Mask null (and not a keyword!) expands to such a null pointer constant.
In the first C ++ construct, only a constant constant expression was allowed as a null pointer constant. Recently, std::nullptr_t was added in C ++.
In C ++, but not in C, a variable a const integral type, initialized by an integral constant expression, is an integral constant expression:
const int c = 3; int i; switch(i) { case c:
So, const char , initialized by the expression '\0' , is a null pointer constant:
int zero() { return 0; } void foo() { const char k0 = '\0', k1 = 1, c = zero(); int *pi; pi = k0;
And you think this is not a sound design?
Updated to include relevant parts of C99 ... According to Β§6.6.6 ...
An integer constant expression must be an integer type and must have only operands that are integer constants, enumeration constants, character constants, sizeof expressions, the results of which are integer constants and floating constants, which are the direct operands of castings. Translation operators in an integer constant expression must convert arithmetic types to integer types, except that part of the operand refers to the sizeof operator.
Some clarifications for C ++ are only programmers:
- C uses the term "constant" for what C ++ programmers know as a "literal".
- In C ++,
sizeof always a compile-time constant; but C has variable length arrays, so sizeof sometimes not a compile-time constant.
Then we see that in Β§6.3.2.3.3 the states ...
An integer constant expression with a value of 0 or such an expression cast for type void * is called a null pointer constant. If the constant of the null pointer is converted to a type of pointer, the resulting pointer, called the null pointer, is guaranteed to compare unevenly with a pointer to any object or function.
To find out how old this functionality is, see identical mirror parts in the C99 standard ...
Β§6.6.6
An integer constant expression must be an integer type and must have only operands that are integer constants, enumeration constants, symbolic constants, sizeof expressions, the results of which are integer constants, and floating constants, which are the direct operands of castings. Cast statements in an integer constant expression must convert only arithmetic types to integer types, except that they are part of the operand for the sizeof operator.
Β§6.3.2.3.3
An integer constant expression with a value of 0 or such an expression cast for type void * is called a null pointer constant. If the constant of the null pointer is converted to a type of pointer, the resulting pointer, called the null pointer, is guaranteed to compare unevenly with a pointer to any object or function.