#defining NULL ...
Mark Brader
msb at sq.uucp
Sat Jun 25 21:47:15 AEST 1988
Peter X Moore:
> >[NULL] is simply a magic cookie defined in stdio.h and returned by some
> >of the stdio functions to signal an error.
Doug Gwyn:
> WRONG. "NULL" has always been intended to represent a null pointer --
> where do you think it got its name?
Me:
Actually, most of the stdio man pages *do* suggest only that NULL is a
magic cookie, for which "the null pointer" was merely the most natural
value, just as -1 is for EOF. To put this into perspective, consider:
FILE _nofile; /* unused */
#define NULL (&_nofile)
If stdio.h contained the above instead of #define NULL 0, the descriptions
in most of the man pages would not have to change. If you do not read them
with the preconception that NULL means a null pointer, it *does* make sense
that it would be a magic cookie which is not equal to any valid file pointer.
And K&R doesn't disprove it either by saying that NULL should mean a null
pointer. What *it* says is, "*We* write NULL instead of zero..." (emphasis
added); it doesn't say that everyone else is required to use that identifier
the same way.
All of which seems to justify Peter's position. However, note that I said
"most" of the man pages. Now check man 3 stdio itself:
# A constant `pointer' NULL (0) denotes no stream at all.
The parenthetic 0 means that Doug is right.
By the way, when I say "the man pages", I'm referring to the V7 man pages,
which are the earliest ones I have available for stdio. The point under
discussion is not how NULL is being used now, but what it was meant to mean
when it was introduced into stdio.h. Similarly, "K&R" refers to the first
edition, not the new one.
Today, of course, NULL is widely used for a null pointer of any kind, and
this widespread practice is correctly enshrined in the Draft ANSI Standard.
As Doug has noted, this requires that NULL be defined as an integral
constant expression of value zero, or such an expression cast to void *.
Mark Brader "The default choice ... is in many ways the most
utzoo!sq!msb important thing. ... People can get started without
msb at sq.com reading a big manual." -- Brian W. Kernighan
More information about the Comp.lang.c
mailing list