NULL is not '\0' (was: Is malloc() or calloc() "better"?)

Karl Heuer karl at haddock.ima.isc.com
Tue Jan 10 04:00:34 AEST 1989


In article <11049 at tiger.oxy.edu> bagpiper at 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 at haddock.isc.com), The Walking Lint
________
% Except in non-prototyped function arguments, where a cast is required.



More information about the Comp.lang.c mailing list