Seven Original Sins of K&R (Long)

Henry Spencer henry at zoo.toronto.edu
Sat Oct 6 02:51:33 AEST 1990


In article <26661 at shamash.cdc.com> bls at u02.svl.cdc.com (Brian Scearce) writes:
>This is the same as #define NULL '\0'
>This is the same as #define NULL 0
>
>Both are different from #define NULL (void *)0
>
>Neither of the suggested #defines are guaranteed to work with
>not-all-bits-0-for-NULL implementations if you pass NULL as a
>parameter to a function with no prototype in scope.

There is *no*, repeat *no*, definition of NULL that is guaranteed to
work with not-all-bits-0-for-NULL implementations if you pass NULL as
a parameter to a function with no prototype in scope.  Actually, this
is true even if null pointers are all-0-bits, because they may not all
be the same size.

Repeat after me, 512 times:

	The representation of different pointer types can be different.

	To turn NULL into a valid null pointer of a particular type, the
	compiler must know the exact type that is desired.

	In the absence of prototypes, the only way to give the compiler
	this information in function calls is to explicitly cast NULL to
	the desired type.

	No definition of NULL can ever remove the need for this, and lazy
	programmers are just going to have to learn to put the casts in.

	No legal program can tell the difference between #define NULL 0,
	#define NULL 0L, and #define NULL ((void *)0).

(Even prototypes do not fully remove the need to be aware of this issue,
since varargs functions still need the casts.)
-- 
Imagine life with OS/360 the standard  | Henry Spencer at U of Toronto Zoology
operating system.  Now think about X.  |  henry at zoo.toronto.edu   utzoo!henry



More information about the Comp.lang.c mailing list