NULL vs 0 vs 0L bikeshed time

Tim Kientzle tim at kientzle.com
Mon Mar 1 14:39:54 PST 2004


Erik Trulsson wrote:
> On Mon, Mar 01, 2004 at 12:07:37PM -0500, Thomas David Rivers wrote:
>>>Mark Murray <mark at grondar.org> writes:
>>>
>>>>I'd like to commit the following patch. It makes sure that for C
>>>>and the kernel, NULL is a ((void *)0)
>>>
>>>This is not correct, because it makes NULL unusable for function
>>>pointers; you can assign 0 to a function pointer, but not (void *)0.
>>  
>> That assignment seems to work... I thought (void *) was assignable to
>> any function pointer...  (Isn't (void *) assignable to any pointer?)
> 
> To any _object_ pointer, not to a function pointer.  Null pointers are
> special however.

I was curious about this, so I looked it up.

ANSI/ISO 9899-1990, Section 6.2.2.3:

  "An integral constant expression with the value
  0, or such an expression cast to type void * is
  called a null pointer constant.  If a null pointer
  constant is assigned to ... a pointer, the
  constant is converted to a pointer of that type.
  Such a pointer ... is gauranteed to compare
  unequal to a pointer to any object or function."

There's some earlier text about void* assignments
that seems to restrict assigning void* to function pointers.
The conclusion I draw from this:

    int (*f)(void);
    void *p;

    f = p;  /* NOT LEGAL: Can't assign void* to function ptr */

    f = (void *)0;  /* LEGAL: Can assign NULL to function ptr */

    f = 0;  /* LEGAL: Can assign NULL to function ptr */


Tim Kientzle



More information about the freebsd-current mailing list