Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!wuarchive!uunet!zephyr.ens.tek.com!tektronix!reed!intelhf!ichips!inews!pima!bhoughto From: bhoughto@pima.intel.com (Blair P. Houghton) Newsgroups: comp.lang.c Subject: Re: Is there a NULL pointer to functions? Message-ID: <4428@inews.intel.com> Date: 27 May 91 20:39:21 GMT References: <1991May21.125639.10052@umiami.ir.miami.edu> <4416@inews.intel.com> <748@taumet.com> Sender: news@inews.intel.com Distribution: na Organization: Intel Corp, Chandler, AZ Lines: 49 In article <748@taumet.com> steve@taumet.com (Stephen Clamage) writes: >bhoughto@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. :-)"