What is NULL? (was "Re: regcmp()")

Richard Hargrove richardh at killer.UUCP
Tue Feb 23 05:10:11 AEST 1988


In article <15100006 at bucc2>, brian at bucc2.UUCP writes:
> 
> > Not if you cast it to "char *", it isn't!  Passing "(char *)0" is quite
> > sufficient, unless the compiler is horribly broken.
> 
>   Tsk, tsk, tsk. This statment is true if sizeof(int) == sizeof(pointer).
> Not a good assumption to make, eh? I do a great deal of programming using
> Microsoft C 5.0 on IBM PEE CEE's. In large model, pointers are four bytes
> long. However, constant integer expessions are ints, and an int is only two
> bytes long.
> 

This issue raises its ugly head periodically on comp.lang.c and generally
causes a lot of consternation until the resident gurus beat it back down.
The well established principle is that casts convert! If pointers are 
four bytes long, the cast should convert the two byte 0 to a four byte
representation of the invalid pointer. In C, the value 0 is guaranteed to
be an invalid pointer, even if the compiler must convert it to some other
form (e.g., 0L). Any compiler that does not do this is indeed broken.

The definition of NULL as 0L by C compilers targeted to the 80x86 is a
kludge attempting to protect ignorant C programmers who don't cast
NULL (which, to the compiler is indistinguishable from 0) when it
is an actual parameter. 

This discussion should move to comp.lang.c since it is now a C issue.

richard hargrove
...!{ihnp4 | codas | cbosgd}!killer!richardh
--------------------------------------------



More information about the Comp.unix.questions mailing list