To dereference or not dereference, that is the question

KW Heuer kwh at bentley.UUCP
Fri Mar 7 08:55:50 AEST 1986


In article <44 at umcp-cs.UUCP> umcp-cs!chris (Chris Torek) writes:
>		int f(), (*p)();
>		p = f;
>		p(1);
>		(*p)(2);
>		(**p)(3);
>		(****************p)(4);

Although there is no excuse for "p(1)", the other three examples are
in fact correct.  Recall that there are only two things you can do
with a function (not a pointer): call it (as in f()) or take its
address (any other use of the name).  Thus "p = f" takes the address
of function f and stores it in pointer p.

Now consider "(**p)(3)".  "*p" is of type _function_, but it is not
being called (since the next operator is "*" rather than "()"), so
we have to take its address.  This leaves us with
	(* pointer_to_function)(3)
which is legal.  Each time you add a "*" you convert a pointer into
a function, but if you don't immediately call it, you are taking its
address again.

Similarly, you could write "(*exit)(0)".  For these reasons I think
the address operator should have been required, "p = &f"; as the
language stands it is permitted but discouraged (lint warns).



More information about the Comp.lang.c mailing list