Is there a NULL pointer to functions?
Stephen Clamage
steve at taumet.com
Mon May 27 04:09:30 AEST 1991
bhoughto at pima.intel.com (Blair P. Houghton) writes:
>In article <1991May21.125639.10052 at umiami.ir.miami.edu> devebw9f at miavax.ir.miami.edu writes:
>>#define NULL ((void *) 0)
>>void foo (void (*fun) (void))
>>{
>> if (fun != NULL) /* Line with the warning. */
>> ...
>>warning: ANSI C forbids comparison of `void *' with function pointer
>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.
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.
The usage
if( fun != 0 ) ...
is correct and IMHO more readable. By "correct", I mean that it is
in strict conformance with ANSI C, and also conforms to early C (K&R1).
When a cast is needed, one can argue about which of these two
(void (*)(void) NULL)
(void (*)(void) 0)
is more readable (modulo whitespace). I don't think either is very
readable, and I might use a typedef for void(*)(void) or a macro
for the whole null pointer.
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.
--
Steve Clamage, TauMetric Corp, steve at taumet.com
More information about the Comp.lang.c
mailing list