Calling functions via pointers

Guy Harris guy at sun.uucp
Sat Nov 1 09:11:47 AEST 1986


> >Is the original format documented anywhere?  Is it "standard" C or not?
> 
> It most certainly is *not* standard, and I would dare say it is sloppy form.

I don't like it either, but it *is* in the current ANSI C draft.  (Then
again, I don't like C's "array expression converts to pointer expression"
rules either.)  See section 3.3.2.2; it says "The expression that denotes
the function shall have type 'pointer to function'.", with a footnote
pointing out that "Most often, this is the result of converting an
identifier that is a function designator."  Thus, in the statement

	foo(x);

where "foo" was earlier declared as a function (or where it is implicitly
declared as a function by reference, but only Don't Be(e)s do this), the
name "foo" immediately gets converted to type "pointer to function", that
"pointer" gets "dereferenced" and the resulting function is called.  In the
example being complained about, in the statement

	if (action(arg1, arg2))
		etc;

the pointer "action" is dereferenced and the function is called.  If you say

	if ((*action)(arg1, arg2))
		etc;

"action" is dereferenced; this yields an object of type "function", but they
can't appear in expressions so it immediately gets converted back to type
"pointer to function"; that pointer value (identical to the value of
"action") is then dereferenced and the function is called.

It does make a perverse kind of sense, I suppose, if you think about it,
just as the similar handling of array types and array-valued expressions
does.  (If anyone wants to object that "C doesn't have array-valued
expressions", I refer them to section 3.2.2.1 in the latest ANSI C draft,
where they speak of "expressions that ha(ve) type 'array of <type>'.")

If you want the rationale for why this was done, check the Rationale
document that came with the latest ANSI C draft.
-- 
	Guy Harris
	{ihnp4, decvax, seismo, decwrl, ...}!sun!guy
	guy at sun.com (or guy at sun.arpa)



More information about the Comp.lang.c mailing list