Pointers to functions

Dr. T. Andrews tanner at cdis-1.compu.com
Fri May 17 23:16:17 AEST 1991


) [ ``new'' function call syntax via pointer derives from c++ usage ]
I don't think your explanation (function-call syntax derives from c++)
is quite sufficient.  I think that ANSI just made things consistant.
Given
	int (*pf)();
	int f();
	int i, j;
	enum { ci=0x45 };
You can reasonably say
	pf = f;			/* pf now points to function f */
because the bare ``f'' has the type ``pointer to function''.
I can also say
	i = ci;			/* i now has value 0x45 */
	i = 0x45;		/* same here.
The types of ``pf'' and ``f'' are compatible.  So are the types
of ``i'' and ``ci'' (because in expressions, enums are ints),
and the types of ``i'' and ``0x45''.

The trick, in both cases, is that I have constants being assigned
to variables.  Both ``ci'' and ``f'' are constant values.  Now, I
can also say:
	i = f();		/* call func at f, stash value in i */
Why should I not declare that ``f'' is really a function pointer,
and that such things are used by following them with parens?  After
all, we've just seen (through assignment to ``pf'') that we can
interpret the value of ``f'' as a function pointer.

If ``f'' is a function pointer, and so is ``pf'', then why should
we not treat them similarly?  In particular, why should we not
also say:
	i = pf();		/* call func at pf, stash value in i */

We do not use different syntax for
	i = 1;
and
	j = i;
do we?

As a sop to history, I note that some compilers accepted this
argument before ANSI; it was ``prior art'' which has simply been
formalized.  Some compilers required no distinction between the
form of call via the pointer ``pf'' and the name ``f''.  Some did
want the syntax ``(*pf)()''.
-- 
uflorida!ki4pv!cdis-1!tanner {uunet dsinc}!cdin-1!cdis-1!tanner



More information about the Comp.lang.c mailing list