Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!ncar!ico!ism780c!haddock!karl From: karl@haddock.ima.isc.com (Karl Heuer) Newsgroups: comp.lang.c Subject: Re: pointers to functions Keywords: pointers, functions, portable Message-ID: <14499@haddock.ima.isc.com> Date: 1 Sep 89 13:54:04 GMT References: <1679@hydra.gatech.EDU> Reply-To: karl@haddock.ima.isc.com (Karl Heuer) Organization: Interactive Systems, Cambridge, MA 02138-5302 Lines: 28 In article <1679@hydra.gatech.EDU> wj4@prism.gatech.EDU (JOYE,WILLIAM A) writes: >is the following code portable and why or why not? First, I'll answer the question you probably meant to ask: "When `f' has type `pointer to function', is `f()' a valid way to invoke it, in addition to `(*f)()'?" The answer is "Yes, but this feature is new to ANSI C." As has already been mentioned, K&R does not guarantee this. (But the conclusion "even the gods can lie" is inappropriate. It's merely the case that some, but not all, pre-ANSI compilers allowed this feature as an extension.) Now, in case you're interested, the answer to the question you asked is "No." >extern void printf; Even if you add the parens (I presume they were dropped by accident when you posted -- I find it hard to believe that so many compilers would accept this as written), the correct declaration is "extern int printf(char *, ...);" (or just use "#include ", which defines it). In pre-ANSI C, omit the arglist. > void (*f)() = printf; Likewise, the correct declaration here is "int (*f)(char *, ...) = printf", or just "int (*f)() = printf" in pre-ANSI C. (It is *not* legal to declare a function void when you don't plan to use its result. You must declare its true type, and then (optionally) cast to void.) Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint