Casting NULL?

gwyn at brl-smoke.UUCP gwyn at brl-smoke.UUCP
Sat Jan 24 08:40:11 AEST 1987


In article <3179 at brl-adm.ARPA> Peter Steele - Acadia writes:
>test( (char *)NULL, (char *)NULL );
>this is required because pointers to different types of objects
>may not be the same size. I can appreciate this but how common
>is it really?

It's quite common, and furthermore NULL is usually 0 (an integer),
which is often of a different size than any pointer.  (Guy Harris,
among others, explains this once or twice a year in this newsgroup.)

Using an X3J11-compliant C implementation, if a function prototype
is in scope when the invocation occurs, the parameters will be
automatically converted to the type defined in the prototype.
(Some of us don't like this feature, by the way, feeling that it
encourages sloppy programming practices.)

The really best way to think while coding C is to treat all data
as strictly-typed, maintain absolute consistency in use of types,
and in the (rare) instances when a type conversion is required,
show it explicitly by using an appropriate cast.  When I adopted
this mental discipline, the reliability of my C code improved
significantly (not to mention its portability).  I even take it a
step farther and maintain a distinction between Boolean and
integer data types (not enforced by the language), using only
Boolean expressions in conditionals, for example.  This is perhaps
the simplest example of the very important general principle of
"data abstraction", which should be explained in good Computer
Science textbooks.



More information about the Comp.lang.c mailing list