Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!think.com!zaphod.mps.ohio-state.edu!wuarchive!uunet!taumet!steve From: steve@taumet.com (Stephen Clamage) Newsgroups: comp.lang.c Subject: Re: Is there a NULL pointer to functions? Message-ID: <748@taumet.com> Date: 26 May 91 18:09:30 GMT References: <1991May21.125639.10052@umiami.ir.miami.edu> <4416@inews.intel.com> Distribution: na Organization: Taumetric Corporation, San Diego Lines: 39 bhoughto@pima.intel.com (Blair P. Houghton) writes: >In article <1991May21.125639.10052@umiami.ir.miami.edu> devebw9f@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@taumet.com