September 25, 2004 11:50 PM
Null as you know means nothing. This whole entry is about nothing. Nothing in C. Heh heh, I've always wanted to say that. Actually, this entry is about null pointers. Thought about them when one of my friends asked if NULL can be substituted with zero.

A null pointer is a special pointer value that implies not allocated yet. It is not supposed to point to any function or value or be equal to a pointer to any object or function. C specifies that a constant 0 in a pointer context is converted internally to a null pointer. But 0 as an argument to a function might1 not. Then, you have to typecast 0 to the corresponding pointer data type that the function is expecting. For example, if the argument required is a char*, then pass (char *)0 instead of just 0.

The internal representation of null pointers for different types may be different. The compiler must be able to make the distinction if necessary.

The macro NULL is used to denote zero the null pointer emphasizing it's difference from an integer zero. Usually, NULL is #defined as zero typecasted to (void *). Though a normal zero is perfectly acceptable. The language defines it to be internally translated anyway.

It's implementation in the GNU C library clarifies things further.

Old MS DOS compilers2 issue a null pointer assignment error when you try to assign a value via a null pointer, that is, when you try to assign something to nothing.

CategoryC


[1] Especially true for functions that take variable arguments or does not have it's prototype in scope.
[2] Like good old Turbo C.

Copyright © 2004-2011 Anirudh Sasikumar. All rights reserved.
Last Updated: January 21, 2005 4:25 PM