Tuesday, June 23, 2009

WHAT IS NULL AND HOW IS IT #DEFINED?

As a matter of style, many people prefer not to have unadorned 0's scattered throughout their programs. For this reason, the preprocessor macro NULL is #defined (by or ), with value 0 (or (void *)0, about which more later). A programmer who wishes to make explicit the distinction between 0 the integer and 0 the null pointer can then use NULL whenever a null pointer is required. This is a stylistic convention only; the preprocessor turns NULL back to 0 which is then recognized by the compiler (in pointer contexts) as before. In particular, a cast may still be necessary before NULL (as before 0) in a function call argument.
(The table under question 2 above applies for NULL as well as 0.)
NULL should _only_ be used for pointers.

No comments:

Post a Comment