prev up next   top/contents search

comp.lang.c FAQ list · Question 5.11

Q: I once used a compiler that wouldn't work unless NULL was used.

A: Unless the code being compiled was nonportable, that compiler was probably broken.

Perhaps the code used something like this nonportable version of an example from question 5.2:

	execl("/bin/sh", "sh", "-c", "date", NULL);	/* WRONG */
Under a compiler which defines NULL to ((void *)0) (see question 5.6), this code will happen to work. [footnote] However, if pointers and integers have different sizes or representations, the (equally incorrect) code
	execl("/bin/sh", "sh", "-c", "date", 0);	/* WRONG */
may not work.

Correct, portable code uses an explicit cast:

	execl("/bin/sh", "sh", "-c", "date", (char *)NULL);
With the cast, the code works correctly no matter what the machine's integer and pointer representations are, and no matter which form of null pointer constant the compiler has chosen as the definition of NULL. (The code fragment in question 5.2, which used 0 instead of NULL, is equally correct; see also question 5.9.) (In general, making decisions about a language based on the behavior of one particular compiler is likely to be counterproductive.)

prev up next   contents search
about this FAQ list   about eskimo   search   feedback   copyright

Hosted by Eskimo North