I couldn't find an answer to this anywhere. I just read K&R and saw them calling a function pointer like this:
(*ptr)(arg1, arg2);
I vividly remember, however, to have seen someone using them like this:
ptr(arg1, arg2);
That might have been in C++, though.
TL;DR
The rules in C and C++ are the same, there's no difference between the two.
What does the C++ Standard (n3797) say?
5.2.2p1
Function call[expr.call]
A function call is a postfix expression followed by parentheses containing a possible empty, comma-separated list of initializer-clauses which constitute the arguments to the function.
The postfix expression shall have function type or pointer to function type.
What does the C standard (n1570) say?
6.3.2.1p4
Lvalues, arrays, and function designatorsA function designator is an expression that has function type. Except when it is the operand of the
sizeof
operator, the_Alignof
operator, or the unary&
operator, a function designator with type "function returning type" is converted to an expression that has type "pointer to function returning type".
6.5.2.2p1
Function callsThe expression that denotes the called function shall have type pointer to function returning
void
or returning a complete object type other than an array type.
Conclusion?
How the rule are expressed differs between C++ and C. In C the implicit function-to-pointer conversion always applies when calling a function, whereas C++ states that the "postfix expression" could be either a pointer, or a variable of function type.
However; your question is if the two ways of calling a function through a pointer differs between C++ and C, and the answer is: No, there's no difference between (1)
, (2)
, and (3)
, nor is there a difference between the two languages.
(*fptr)(123); // (1)
fptr(123); // (2)
(***fptr)(123); // (3)
Note: Be aware that the there's no difference between (*fptr) (...)
and fptr (...)
when it comes to calling a function, but that they can be vastly different in other contexts.
How are the rules? Do they differ in C and C++?
No. Dereferencing the function pointer in this context was never necessary, as it is redundant.
However, you might still want to use the asterisk-notation to underline that you are calling a function through a function pointer. That may also be the reason for the author of the snippet in your post.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With