pointers to functions

Karl Heuer karl at haddock.ima.isc.com
Fri Sep 1 23:54:04 AEST 1989


In article <1679 at hydra.gatech.EDU> wj4 at 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 <stdio.h>", 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 at haddock.isc.com), The Walking Lint



More information about the Comp.lang.c mailing list