Do nil pointers convert to nil pointers?

Adam S. Denton asd at cbnewsj.att.com
Wed Mar 20 09:42:19 AEST 1991


Let's say I have a "generic" function that might work on different data types.
This function might look like this:

    void generic_func(void *objectp, size_t size);

The intended call would be like this:

    T *x;    /* T is some type */

    ...
    generic_func( (void *)x, sizeof(T) );   /* cast included for clarity */
    ...

Now, suppose generic_func() can be called with a nil pointer; i.e.,
x can be NULL (make that (T *)0).

For this to be useful, generic_func would have some code such as
    ...
    /* Note, the following comparison compares "void *"s, not "T *"s!!! */
    if (objectp == NULL) {...};
    ...

When generic_func is called, the nil (T *) argument x will be converted
to (void *) before generic_func gets it.  My question is, is this
guaranteed to produce a *nil* generic pointer, or not?  In other words,
does ANSI guarantee that (void *)0 compares equal to (void *)(T *)0,
or is this just wishful thinking on my part?

On a related note, can anything be said for (T1 *)0 vs. (T1 *)(T2 *)0?
(Not that converting arbitrary pointers is an acceptable practice... :-))

Thanks in advance,

Adam Denton
asd at mtqua.att.com



More information about the Comp.lang.c mailing list