[comp.lang.c] NULL is not '\0'

karl@haddock.ima.isc.com (Karl Heuer) (01/10/89)

In article <11049@tiger.oxy.edu> bagpiper@oxy.edu (Michael Paul Hunter) writes:
>I think it would be better (and faster) to malloc and set the first element
>of the array to (char)NULL if you want it to be a 0 length string.

Right idea, wrong name.  NULL is a null pointer constant.  Please do not
confuse it with the null character.  If the built-in name '\0' isn't good
enough, then call it NUL (with one L, as in the ASCII tables).

For nearly accidental reasons, it is quite legal% to write '\0' when you mean
a null pointer constant, though only an Obfuscated C contestant would do so.
Using NULL when you mean a null character, however, is not guaranteed to work
(even with the cast), since (in ANSI C) the macro might expand into (void *)0.

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint
________
% Except in non-prototyped function arguments, where a cast is required.

jpr@dasys1.UUCP (Jean-Pierre Radley) (01/27/89)

In article <11375@haddock.ima.isc.com> karl@haddock.ima.isc.com (Karl Heuer) writes:
>Right idea, wrong name.  NULL is a null pointer constant.  Please do not
>confuse it with the null character.  If the built-in name '\0' isn't good
>enough, then call it NUL (with one L, as in the ASCII tables).
>For nearly accidental reasons, it is quite legal% to write '\0' when you mean
>a null pointer constant, though only an Obfuscated C contestant would do so.
>Using NULL when you mean a null character, however, is not guaranteed to work
>(even with the cast), since (in ANSI C) the macro might expand into (void *)0.

Well, I just wasted a lot of time with one my programs because of the
following:

On the Tandy 6000, the stdio.h file says:
	#define NULL	0

But I just switched to a Tandy 4000, and the SCO 2.2.4 Xenix stdio.h says:
	#define NULL	(char *)0

"... standards, because there are so many ..."
-- 
Jean-Pierre Radley		Honi soit		jpr@dasys1.UUCP
New York, New York		qui mal			...!hombre!jpradley!jpr
CIS: 76120,1341			y pense			...!hombre!trigere!jpr

gwyn@smoke.BRL.MIL (Doug Gwyn ) (01/27/89)

In article <8468@dasys1.UUCP> jpr@dasys1.UUCP (Jean-Pierre Radley) writes:
>Well, I just wasted a lot of time with one my programs because of the
>following:
>On the Tandy 6000, the stdio.h file says:
>	#define NULL	0
>But I just switched to a Tandy 4000, and the SCO 2.2.4 Xenix stdio.h says:
>	#define NULL	(char *)0
>"... standards, because there are so many ..."

Karl's advice was correct.

The second definition of NULL you exhibit above is ALMOST correct
but not quite.  It probably dates from before X3J11 specified clearly
what NULL had to be (before that, strictly speaking only the first
definition you exhibit was correct, but many implementors didn't
appreciate that).

What do you mean, "there are so many"?  Tandy Xenix implementations
are not C "standards" by any stretch of the imagination.

davidsen@steinmetz.ge.com (William E. Davidsen Jr) (01/30/89)

In article <9515@smoke.BRL.MIL> gwyn@brl.arpa (Doug Gwyn (VLD/VMB) <gwyn>) writes:

| What do you mean, "there are so many"?  Tandy Xenix implementations
| are not C "standards" by any stretch of the imagination.

  I think in this case he was referring to K&R 1st ed and dpAns. I
pointed out to someone who posted a flame in response to a bug fix that
it is unwise to assume the type of NULL, and desirable to cast it except
when used for assignment to or comparison with a pointer.
-- 
	bill davidsen		(wedu@ge-crd.arpa)
  {uunet | philabs}!steinmetz!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me