Function pointers and NULL

  • Thread starter Richard Heathfield
  • Start date
R

Richard Heathfield

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.
 
H

Harald van =?UTF-8?B?RMSzaw==?=

Richard said:
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.
 
R

Richard Heathfield

Harald van D?k said:
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. :)
 
M

Malcolm McLean

Richard Heathfield said:
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.
 
H

Harald van =?UTF-8?B?RMSzaw==?=

Malcolm said:
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.
 
M

Malcolm McLean

Harald van D?k said:
(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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,422
Latest member
LatashiaZc

Latest Threads

Top