prev up next   top/contents search

comp.lang.c FAQ list · Question 4.12

Q: I've seen different syntax used for calling functions via pointers. What's the story?


A: Originally, a pointer to a function had to be ``turned into'' a ``real'' function, with the * operator, before calling:

	int r, (*fp)(), func();
	fp = func;
	r = (*fp)();
The interpretation of the last line is clear: fp is a pointer to function, so *fp is the function; append an argument list in parentheses (and extra parentheses around *fp to get the precedence right), and you've got a function call.

It can also be argued that functions are always called via pointers, and that ``real'' function names always decay implicitly into pointers (in expressions, as they do in initializations; see question 1.34). This reasoning means that

	r = fp();
is legal and works correctly, whether fp is the name of a function or a pointer to one. (The usage has always been unambiguous; there is nothing you ever could have done with a function pointer followed by an argument list except call the function pointed to.)

The ANSI C Standard essentially adopts the latter interpretation, meaning that the explicit * is not required, though it is still allowed.

See also question 1.34.

References: K&R1 Sec. 5.12 p. 116
K&R2 Sec. 5.11 p. 120
ISO Sec. 6.3.2.2
Rationale Sec. 3.3.2.2
H&S Sec. 5.8 p. 147, Sec. 7.4.3 p. 190


prev up next   contents search
about this FAQ list   about eskimo   search   feedback   copyright

Hosted by Eskimo North