Re: NULL vs 0 vs 0L bikeshed time

From: Tim Kientzle <tim_at_kientzle.com>
Date: Mon, 01 Mar 2004 14:39:06 -0800
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
Received on Mon Mar 01 2004 - 13:39:54 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:37:45 UTC