Value of a null pointer

Doug Gwyn gwyn at brl-smoke.ARPA
Mon Apr 25 04:41:28 AEST 1988


In article <4728 at cup.portal.com> Paul_L_Schauble at cup.portal.com writes:
>He cites two reasons:
>1. An uninitialized external is set to zero bits. C defines an uninitialized
>   external pointer to be a null pointer. Therefore zero bits must be a null
>   pointer.

No, an uninitialized external datum is automatically initialized with
a zero of the right type.  The representation of this zero need not be
all zero bits, and it necessarily depends on the type.

>2. One can use calloc to allocate an array of pointers. The initial value of
>   this array must be null pointers. calloc sets the area to zero bits.
>   Therefore.......

calloc() initializes the allocated storage to zero byte (char) values.
That may or may not be suitable for interpretation as an array of
zero floating-point or null pointer values, depending on the architecture.

>    ptr_type_var = (ptr_type)0;     /* assigns null pointer */
>    ptr_type_var = 0;               /* assigns null pointer */
>    func( (ptr_type)0 );            /* function expecting pointer type */
>Are all of these always correct?

Yes, those are correct uses of null pointers.

The confusion probably arises because the following:
	0
can be used to initialize a pointer variable with a null pointer,
and it also will compare equal to a null pointer of any type.
The thing to realize is that the compiler may have to map that
apparently-integer-constant 0 into some strange internal form
when necessary to properly match the way that pointers are
represented.



More information about the Comp.lang.c mailing list