Is there a NULL pointer to functions?

Blair P. Houghton bhoughto at pima.intel.com
Tue May 28 06:39:21 AEST 1991


In article <748 at taumet.com> steve at taumet.com (Stephen Clamage) writes:
>bhoughto at pima.intel.com (Blair P. Houghton) writes:
>>Use a cast to get the correct type in the right-hand operand:
>
>>	if ( fun != (void (*) (void))NULL )
>
>This isn't really the best solution.  NULL as defined in the standard
>ANSI headers is a pointer to an object (data), not pointer to a function.

Not so:

"...NULL, which expands to an implementation-defined null
pointer constant..."
(ANSI X3.159-1989, sec. 4.1.5, p. 99, ll. 21-22)

"Such a pointer, called a null pointer constant, is guaranteed to
compare unequal to a pointer to any object OR FUNCTION." [emphasis
mine --Blair]
(Ibid, sec. 3.2.2.3, p. 38, ll. 3-4)

>Where you need a null pointer to a function, use a literal 0, or a 0
>cast to the appropriate type when calling a non-prototyped function.
[...]
>BTW, the original example shows a programmer #define for NULL.  Since
>NULL is a required macro in many ANSI standard headers, it is not a good
>idea to #define it yourself.  You may run into a conflict with what is
>in the headers on some other system.

You have this right, but the specifications of `NULL' and `null pointer
constant' pretty much make

	#define NULL 0

the only valid definition of `NULL,' although there may be others.
Using `(void *) 0' as the value, however, will break the requirement
in section 3.2.2.3, as shown above, since it won't compare legally
at all with a function pointer, while the bare `0' will.

>When a cast is needed, one can argue about which of these two
>	(void (*)(void) NULL)
>	(void (*)(void) 0)

I argue "neither!" for punctuary reasons.  :-)

				--Blair
				  "One of us has done too many
				   Margaritas this weekend, and
				   I've only had six since Friday,
				   so it ain't me.  :-)"



More information about the Comp.lang.c mailing list