[comp.lang.c] #defining NULL as

minow@thundr.dec.com (Martin Minow THUNDR::MINOW ML3-5/U26 223-9922) (06/22/88)

In a recent posting, sun!gorodish!guy comments on another posting:

>> ...if "0" does refer to the null pointer, why do some systems have #define
>> NULL (-1) in stdio?
 
>Because the implementors were ignorant.  Any language implementation that
>defines NULL as -1, and purports to be an implementation of C, is broken.

While this is true as far as it goes, the documentation for early Unix
systems were not always careful to explain that NULL was zero.  For
example, the description of fopen() in 4.1bsd states that it returns
"the pointer NULL" if the file cannot be accessed.  It does not, however,
note that the value of NULL is zero.  One could, therefore, conceive of
a properly-defined Unix stdio package where the error return value from
fopen() was non-zero.

This means that the proper way to open a file is

	if ((fd = fopen(...)) != NULL) {
	    ...
	}

Of course, the shave-a-byte-off-the-source-code weenies would probably
write
	if ((fd = fopen(...)) {
	
which requires the error value to be zero and all stdio implementations
I'm familiar with do define it as zero.

Somewhere in the distant past, I recommended to the Ansi committee that
NULL should have been made a reserved word with undefined content.  They
rejected this request, as they didn't want to add any reserved words to
the language.

Martin Minow
minow%thundr.dec@decwrl.dec.com

jgk@speech2.cs.cmu.edu (Joe Keane) (06/23/88)

In article <8806221521.AA00536@decwrl.dec.com> minow@thundr.dec.com (Martin Minow THUNDR::MINOW ML3-5/U26 223-9922) writes:
>In a recent posting, sun!gorodish!guy comments on another posting:
>>[`#define NULL (-1)' is unacceptable]

>While this is true as far as it goes, the documentation for early Unix
>systems were not always careful to explain that NULL was zero.

Of course not, since it's explained in K&R.

>Of course, the shave-a-byte-off-the-source-code weenies would probably
>write
>	if (fd = fopen(...)) {

Weenies like Dennis Ritchie, i suppose?  It's used all through K&R,
what more do you want?

>Somewhere in the distant past, I recommended to the Ansi committee that
>NULL should have been made a reserved word with undefined content.  They
>rejected this request, as they didn't want to add any reserved words to
>the language.

That's not the right reason.

--Joe

moore@ic.berkeley.edu (Peter X Moore) (06/23/88)

Please, there are TWO issues: NULL and the integer constant 0. 
In pre-ANSI C, NULL HAS NO SPECIAL MEANING. It is not, REPEAT NOT,
part of the language in any way, shape, or form.  It is simply a
magic cookie defined in stdio.h and returned by some of the stdio functions
to signal an error.  Now most sane systems defined it to be 0, since 
assigning the constant 0 to a pointer IS guarenteed by the language to 
yeild a value that doesnt point to any valid object.  Therefore it has
been the habit of many programmers to use NULL as their distinguished
pointer value, relying on NULL being 0.  This was/is a bad idea, NULL 
didn't have to be 0.  And don't you feel strange having to import 
stdio.h into a file just to do pointer manipulation?

(Before you respond that K&R blessed such behavior, look again.  All the 
examples I have seen, they explicitly #define NULL to be 0 before using
it in any of their example code.)

ANSI evidently recognized that too many people have written too much 
code with this assumption, so they blessed it. In ANSI C NULL is defined
as the null pointer constant and defined in stddef.h instead of stdio.


	Peter Moore
	moore@Berkeley.EDU
	...!ucbvax!moore

gwyn@brl-smoke.ARPA (Doug Gwyn ) (06/23/88)

In article <4036@pasteur.Berkeley.Edu> moore@ic.berkeley.edu (Peter X Moore) writes:
>[NULL] is simply a magic cookie defined in stdio.h and returned by some
>of the stdio functions to signal an error.

WRONG.  "NULL" has always been intended to represent a null pointer --
where do you think it got its name?  Functions that return pointers can
return either a valid pointer or a null pointer, so that NULL in fact is
often useful as the return value to indicate an unsuccessful operation.
But the more general meaning is simply "null pointer".