Invoking pointers to functions (C sytle)

Karl Heuer karl at ima.isc.com
Sun Dec 9 08:40:57 AEST 1990


Martin Weitzel said pretty much what I wanted to, so I'll just describe the
situation from a different angle.

Function-call semantics in ANSI C may be described by either of two models
(here f is a function and pf is a pointer-to-function):

(a) Functions decay into pointers in any rvalue context, and there are no
interesting lvalue contexts for them, so they are practically a nonexistent
type.  The interesting operations are
	(): pointer -> result-type
	&: function -> pointer, but this is redundant
	*: pointer -> function, but it will immediately decay back
In the expression `pf = f' the two sides have matching type because of the
decay of `f' (unlike `f = pf' which would be an attempt to use `f' in an
illegal lvalue context).  `pf()' has the obvious meaning, and `f()' also works
since `f' decays to the same type as `pf' before the `()' operator applies.

(b) Functions and pointers are logically distinct types.  The interesting
operations are
	(): function -> result-type
	&: function -> pointer
	*: pointer -> function
The expressions `pf = &f', `f()', and `(*pf)()' have the obvious meanings.
As a special kludge, uttering the name `f' in a context other than as an
operand of `&' or `()' causes an implicit conversion to pointer (as if by
`&'), and uttering the name `pf' in a call context causes an implicit
conversion to function (as if by `*').

Since the two models produce identical results, either can be said to be
correct.  (The fact that the ANS describes the world in terms of model (a) is
not important.)  I happen to think model (b) is cleaner, so that's the one I
use.

Karl W. Z. Heuer (karl at ima.isc.com or uunet!ima!karl), The Walking Lint



More information about the Comp.lang.c mailing list