Casting NULL again

Gregory Smith greg at utcsri.UUCP
Thu Jan 29 03:12:42 AEST 1987


In article <3859 at brl-adm.ARPA> Peter at adm.UUCP writes:
>Thanks for your comments on this matter. Unfortunately, most
>of you misunderstood what I wanted. I realize that under some
>machines ints are 16 bits and pointers are 32. I have a MAC and
>thats the case there and I often have to pass parameters as
>(long)NULL to get the right number of bytes passed. That's
>nothing to do with pointers per se,=that's a "problem" of
???
If you are passing a long 0 to a function, you indeed should be casting
it to long:
	fseek( file, (long)0, 0 );

Or, use a long 0 constant:
	fseek( file, 0L, 0 );

but (long)NULL ? If you are using this to pass a 0L, then it is a misleading
use of the NULL symbol. If you are passing a NULL pointer and using a (long)
cast to make it the right size, you are making a mistake.

lint will tell you about this mistake; a function expects a pointer and is
being passed a long. There are machines where a long is not the same size as
a pointer, and there may well be machines where (long)0 does not give the
same bit pattern as a NULL pointer. If you are passing a NULL pointer
to a function expecting a (char *), just write (char *)NULL. Guaranteed to
work, no extra trouble, more self-explanatory.
-- 
----------------------------------------------------------------------
Greg Smith     University of Toronto      UUCP: ..utzoo!utcsri!greg
Have vAX, will hack...



More information about the Comp.lang.c mailing list