prev up next   top/contents search

comp.lang.c FAQ list · Question 5.6

Q: If NULL were defined as follows:

	#define NULL ((char *)0)
wouldn't that make function calls which pass an uncast NULL work?

A: Not in the most general case. The complication is that there are machines which use different internal representations for pointers to different types of data. The suggested definition would make uncast NULL arguments to functions expecting pointers to characters work correctly, but pointer arguments of other types could still (in the absence of prototypes) require explicit casts. Furthermore, legal constructions such as

	FILE *fp = NULL;
could fail.

Nevertheless, ANSI C allows the alternate definition

	#define NULL ((void *)0)
for NULL. [footnote] Besides potentially helping incorrect programs to work (but only on machines with homogeneous pointers, thus questionably valid assistance), this definition may catch programs which use NULL incorrectly (e.g. when the ASCII NUL character was really intended; see question 5.9). See also question 5.7.

At any rate, ANSI function prototypes ensure that most (though not quite all; see question 5.2) pointer arguments are converted correctly when passed as function arguments, so the question is largely moot.

Programmers who are accustomed to modern, ``flat'' memory architectures may find the idea of ``different kinds of pointers'' very difficult to accept. See question 5.17 for some examples.

References: Rationale Sec. 4.1.5

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

Hosted by Eskimo North