Interview Questions

This is strange. NULL is guaranteed to be 0, but the null pointer is not?

C Interview Questions and Answers


(Continued from previous question...)

This is strange. NULL is guaranteed to be 0, but the null pointer is not?

When the term ``null'' or ``NULL'' is casually used, one of several things may be meant:
1. The conceptual null pointer,the ``null pointer''--which is distinguishable from all other pointer values and which is ``guaranteed to compare unequal to a pointer to any object or function.'' That is, a null pointer points definitively nowhere; it is not the address of any object or function.
2. The internal (or run-time) representation of a null pointer, which may or may not be all-bits-0 and which may be different for different pointer types. The actual values should be of concern only to compiler writers. Authors of C programs never see them, since they use...
3. The null pointer constant, which is a constant integer 0. It is often hidden behind...
4. The NULL macro, which is #defined to be 0 . Finally, as red herrings, we have...
5. The ASCII null character (NUL), which does have all bits zero, but has no necessary relation to the null pointer except in name; and...
6. The ``null string,'' which is another name for the empty string (""). Using the term ``null string'' can be confusing in C, because an empty string involves a null ('\0') character, but not a null pointer, which brings us full circle...
In other words, to paraphrase the White Knight's description of his song in Through the Looking-Glass, the name of the null pointer is ``0'', but the name of the null pointer is called ``NULL'' (and we're not sure what the null pointer is).
This document uses the phrase ``null pointer'' (in lower case) for sense 1, the token ``0'' or the phrase ``null pointer constant'' for sense 3, and the capitalized word ``NULL'' for sense 4.

(Continued on next question...)

Other Interview Questions