Function pointers and NULL

  • Follow


Is it legal to assign NULL to a function pointer?

I believe it is, because of {3.3(C89)|6.5(C99)}.16.1, Constraint #5:

 * the left operand is a pointer and the right is a null pointer 
constant.

(it doesn't say "object pointer", just "pointer"), but nevertheless I am 
mildly nervous.

If necessary, I could do this:

void null(void);

#define FUNCTION_NULL null

but I'd rather avoid that if I can, because I'd have to add a cast to 
just about every usage thereof.

-- 
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
0
Reply rjh (10789) 7/1/2007 12:11:35 AM

Richard Heathfield wrote:
> Is it legal to assign NULL to a function pointer?

Yes, that is legal for the reason you have stated. Regardless of whether
NULL is of an integer type or of a void pointer type, it can be converted
implicitly to any function pointer type.
0
Reply truedfx (1926) 7/1/2007 12:20:35 AM


Harald van D?k said:

> Richard Heathfield wrote:
>> Is it legal to assign NULL to a function pointer?
> 
> Yes, that is legal for the reason you have stated. Regardless of
> whether NULL is of an integer type or of a void pointer type, it can
> be converted implicitly to any function pointer type.

Thanks. That's what I figured, too, but it's good to have it confirmed 
by a bright bunny. :-)

-- 
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
0
Reply rjh (10789) 7/1/2007 12:43:39 AM

"Richard Heathfield" <rjh@see.sig.invalid> wrote in message 
news:j6-dnX8BJ5osbRvbnZ2dnUVZ8tGqnZ2d@bt.com...
> Is it legal to assign NULL to a function pointer?
>
> I believe it is, because of {3.3(C89)|6.5(C99)}.16.1, Constraint #5:
>
> * the left operand is a pointer and the right is a null pointer
> constant.
>
> (it doesn't say "object pointer", just "pointer"), but nevertheless I am
> mildly nervous.
>
> If necessary, I could do this:
>
> void null(void);
>
> #define FUNCTION_NULL null
>
> but I'd rather avoid that if I can, because I'd have to add a cast to
> just about every usage thereof.
>
It's a glitch.
NULL is allowed to be defined as (void *) 0, which the compiler is then 
allowed to reject as a function pointer. However fptr = NULL has to be 
accepted. So a compiler needs a little patch if it enforces strict type 
checking.

-- 
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

0
Reply regniztar (3128) 7/1/2007 6:18:15 AM

Malcolm McLean wrote:
> "Richard Heathfield" <rjh@see.sig.invalid> wrote in message
> news:j6-dnX8BJ5osbRvbnZ2dnUVZ8tGqnZ2d@bt.com...
>> Is it legal to assign NULL to a function pointer?
>>
>> I believe it is, because of {3.3(C89)|6.5(C99)}.16.1, Constraint #5:
>>
>> * the left operand is a pointer and the right is a null pointer
>> constant.
>>
>> (it doesn't say "object pointer", just "pointer"), but nevertheless I am
>> mildly nervous.
>>
>> If necessary, I could do this:
>>
>> void null(void);
>>
>> #define FUNCTION_NULL null
>>
>> but I'd rather avoid that if I can, because I'd have to add a cast to
>> just about every usage thereof.
>>
> It's a glitch.
> NULL is allowed to be defined as (void *) 0, which the compiler is then
> allowed to reject as a function pointer.

(void *) 0 is a null pointer constant, and any null pointer constant can be
converted to any pointer type, including function pointer types.
0
Reply truedfx (1926) 7/1/2007 7:25:33 AM

"Harald van D?k" <truedfx@gmail.com> wrote in message 
news:f67pqg$u12$1@news4.zwoll1.ov.home.nl...
> Malcolm McLean wrote:
>> "Richard Heathfield" <rjh@see.sig.invalid> wrote in message
>> news:j6-dnX8BJ5osbRvbnZ2dnUVZ8tGqnZ2d@bt.com...
>>> Is it legal to assign NULL to a function pointer?
>>>
>>> I believe it is, because of {3.3(C89)|6.5(C99)}.16.1, Constraint #5:
>>>
>>> * the left operand is a pointer and the right is a null pointer
>>> constant.
>>>
>>> (it doesn't say "object pointer", just "pointer"), but nevertheless I am
>>> mildly nervous.
>>>
>>> If necessary, I could do this:
>>>
>>> void null(void);
>>>
>>> #define FUNCTION_NULL null
>>>
>>> but I'd rather avoid that if I can, because I'd have to add a cast to
>>> just about every usage thereof.
>>>
>> It's a glitch.
>> NULL is allowed to be defined as (void *) 0, which the compiler is then
>> allowed to reject as a function pointer.
>
> (void *) 0 is a null pointer constant, and any null pointer constant can 
> be
> converted to any pointer type, including function pointer types.
>
That's the patch, in words rather than in code.

-- 
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

0
Reply regniztar (3128) 7/1/2007 7:43:07 AM

5 Replies
47 Views

(page loaded in 0.085 seconds)


Reply: