[net.unix-wizards] Using NULL as an illegal pointer

elvy@harvard.ARPA (Marc Elvy) (07/02/84)

While it does not seem to be standard practice in the UNIX world,
all of the C programmers (systems programmers or otherwise) here
have been encouraged to adopt explicit casting of NULL.  Sure, 0
is defined to be an illegal pointer, and every implementation of
NULL which I have ever seen has it equal to 0, but just in case
there exists a machine on which NULL is not an untyped (multityped?)
entity, we cast everything.  Besides, it is good documentation,
and does not break anything.  So my vote goes for

	char *string = (char *) NULL;		and
	return ((char *) NULL);			and
	return ((struct struct_name *) NULL);	etc.

rather than

	char *string = NULL;			and
	return (NULL);

Marc


		      Marc A. Elvy  ( elvy@harvard.{arpa,uucp} )
			     Aiken Computation Laboratory
				  Harvard University

chris@umcp-cs.UUCP (07/02/84)

You can argue the merits of ``char *foo = NULL;'' versus ``char
*foo = (char *)NULL;'' 'til you're blue in the face, but the C
language specification requires that assignments (and return values)
be of the same type as the left hand side of the assignment (or
the type of the function).  If a C compiler generate different code
for

	char *func() { return 0; }

and

	char *func() { return (char *) 0; }

then it is broken.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci (301) 454-7690
UUCP:	{seismo,allegra,brl-bmd}!umcp-cs!chris
CSNet:	chris@umcp-cs		ARPA:	chris@maryland

pete@lvbull.UUCP (Pete Delaney - Rockey Mountain UNIX Consultants) (07/05/84)

Don't forget us word addressing folks, we have pointers that change length
as a function of environment(stack vs arg) and type (int vs char).  We use
two types of NULL pointers:

#define NULL		(char *)0	/* Default: worst case BASE + INDEX */
#define NULL_PTR	(int *)0	/* NOT Pointer to char */

All that kernel trapping code that assumes environment independence of pointers
is also fun to deal with.  Wish (Honeywell) Bull would switch to BYTE addressing.
					Pete Delaney

ron@brl-tgr.ARPA (Ron Natalie <ron>) (07/09/84)

Well you word addressing people have bogus C compilers.  You shouldn't
need to have to cast ZERO to a particular pointer type regardless of
machine architecture, zero can be assigned or compared without casting
at all.

-Ron