Tom St Denis said:
I thought C99 defined it literally as
#define NULL ((void*)0)
That's one legal definition. Another is 0. Another is
((void*)(0ULL). Another is ('/'/'/'-'/'/'/').
Which would be the zero address to me.
(Here we go again.)
C99 6.3.2.3p3:
An integer constant expression with the value 0, or such an
expression cast to type void *, is called a _null pointer
constant_. 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.
(This has not changed significantly since C90.)
Note that a "null pointer constant" is a source code construct;
a "null pointer" is a value that exists during execution of the
program.
The conversion may be non-trivial. An implementation can choose to
treat (void*)0x00000000 as a valid address, and (void*)0xdeadbeef
as a null pointer. Then converting a null pointer constant to void*
would yield a null pointer of type void*, which might have the same
representation as, say, the unsigned int value 0xdeadbeef.
If C had been designed slightly differently, there might be
a keyword, say "null" that represents (and is the only way to
represent) a null pointer constant. There would be no implication
that a null pointer has any particular representation. The semantics
of an if statement would be slightly expanded to say that, if the
expression is of pointer type, it's compared to null rather than
to 0.
In fact, C does pretty much the same thing, except that the syntax
of a null pointer constant is as described in C99 6.3.2.3p3 rather
than a simple keyword.
Oh, and the macro NULL expands to "an implementation-defined null
pointer constant". This can actually cause portability problems,
since NULL can be of type void* or of any integer type. So if you
want to pass NULL as a variadic function argument, you need to cast
it to a pointer type.
See also section 5 of the comp.lang.c FAQ, <
http://www.c-faq.com/>.
<DIGRESSION>
A very strict reading of the standard implies that ((void*)0) is
not a null pointer constant, and therefore not a valid definition
for NULL. The problem is that C99 6.5.1p5 neglects to say that
a parenthesized null pointer constant is a null pointer constant.
This is clearly an unintentional oversight As far as I know all
C compilers accept ((void*)0) as a null pointer constant, and many
have "#define NULL ((void*)0)" in <stddef.h>.
</DIGRESSION>