pointer checking

Bill Crews bc at halley.UUCP
Sun Feb 21 16:00:49 AEST 1988


All right, I know and I am sure most of you out there know that Jan is
supplying us with incorrect information in this message.  Please let's not
follow up (in THIS group, anyway) on this subject.  I have directed follow-ups
to comp.lang.c, but I discourage postings there, too, because this subject
comes up every couple of MONTHS, it seems.  This is one of those rare instances
where one side is RIGHT and the other side WRONG.  If you really don't
understand why he (she?) is wrong, please REPLY to me, read K&R CAREFULLY, or
consult your local guru.  Thanks.

In article <779 at zippy.eecs.umich.edu> janc at palam.eecs.umich.edu (Jan Wolter) writes:

>NULL is a pointer with an illegal value.  In some architectures, the address
>0 is a perfectly reasonable address, so it is concievabale that someone might
>set up a system with NULL defined as ((char *) -1), or some such.
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

If it does, it is DEAD WRONG!  The 0 is interpreted by the COMPILER and by the
LIBRARY, NOT by the machine!  NULL is NOT an ADDRESS!

>                                                    NULL should be defined
>as (char *)0.  (Some 32-bit systems define NULL as 0, which just helps
>perpetuate the confusion between integers and pointers which Vax programmers
>seem to sufferer from).

(char *) 0 is no better that just 0.  If I were going to cast it to anything, I
might select (void *) 0, but it really wouldn't solve any problems.  The
programmer simply must know to cast NULL in argument lists, structure member
lists (when you care to match external data), etc.  Note that, at least in
general, casting NULL in EXPRESSIONS is NOT required.

>	execl(name,arg0,arg1, ..., argn, 0)
>	char *name, *arg0, *arg1, ..., *argn;
>
>which is dead wrong.  That should be a NULL, not a 0.

Wrong, also.  Using just NULL leaves the type of the argument up in the air.
You are right that 0 is wrong, though.  Only (char *) NULL [or (char *) 0] is
right.  [Note that (void *) 0 would be wrong here, also.]  I pick (char *)
because that is the type execl() is expecting.  We wouldn't want to disappoint
execl(), now would we?

-bc
-- 
Bill Crews                                   Tandem Computers
bc at halley.UUCP                               Austin, Texas
..!rutgers!im4u!halley!bc                    (512) 244-8350



More information about the Comp.sources.bugs mailing list