Path: utzoo!censor!geac!torsqnt!news-server.csri.toronto.edu!cs.utexas.edu!samsung!rex!uflorida!gatech!bloom-beacon!deccrl!news.crl.dec.com!decvax.dec.com!ima!dirtydog!karl From: karl@ima.isc.com (Karl Heuer) Newsgroups: comp.lang.c Subject: Re: Invoking pointers to functions (C sytle) Message-ID: <1990Dec08.214057.6280@dirtydog.ima.isc.com> Date: 8 Dec 90 21:40:57 GMT References: <6379@harrier.ukc.ac.uk> <1990Dec02.204212.15465@slate.mines.colorado.edu> <989@mwtech.UUCP> Sender: news@dirtydog.ima.isc.com (NEWS ADMIN) Reply-To: karl@ima.isc.com (Karl Heuer) Organization: Interactive Systems Lines: 34 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@ima.isc.com or uunet!ima!karl), The Walking Lint