I couldn't find an answer to this anywhere. I just read K&R and saw them calling a function pointer like this:
我在任何地方都找不到答案。我刚刚读了K&R,看到他们像这样调用函数指针:
(*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.
不过,这可能是用c++编写的。
- How are the rules?
- 规则怎么样?
- Do they differ in C and C++?
- C和c++有什么不同吗?
2 个解决方案
#1
15
TL;DR
博士TL;
The rules in C and C++ are the same, there's no difference between the two.
C和c++中的规则是一样的,两者之间没有区别。
What does the C++ Standard (n3797) say?
c++标准(n3797)说了什么?
5.2.2p1
Function call[expr.call]
5.2.2p1函数调用(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.
函数调用是后缀表达式,后跟圆括号,括号中可能包含空的、逗号分隔的initializer-子句列表,它们构成函数的参数。
The postfix expression shall have function type or pointer to function type.
后缀表达式应该具有函数类型或指向函数类型的指针。
What does the C standard (n1570) say?
C标准(n1570)说什么?
6.3.2.1p4
Lvalues, arrays, and function designators6.3.2.1p4 lvalue、数组和函数指示器
A 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".函数指示器是具有函数类型的表达式。除了作为sizeof运算符、_Alignof运算符或一元&运算符的操作数时,具有“函数返回类型”的函数指示器被转换为具有“函数返回类型指针”的表达式。
6.5.2.2p1
Function calls6.5.2.2p1函数调用
The 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.表示被调用函数的表达式应该具有返回void或返回除数组类型之外的完整对象类型的类型指针。
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.
在C语言中,隐式函数-指针转换在调用函数时总是适用,而c++则声明“后缀表达式”可以是指针,也可以是函数类型的变量。
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.
然而;您的问题是,如果在c++和C之间通过指针调用函数的两种方式不同,那么答案是:不,(1)、(2)和(3)之间没有区别,这两种语言之间也没有区别。
(*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.
注意:在调用函数时,要注意(*fptr)(…)和fptr(…)之间没有区别,但是在其他上下文中它们可能有很大的不同。
#2
3
How are the rules? Do they differ in C and C++?
规则怎么样?C和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.
但是,您可能仍然希望使用星号标记来强调您正在通过函数指针调用一个函数。这也可能是本文中代码片段作者的原因。
#1
15
TL;DR
博士TL;
The rules in C and C++ are the same, there's no difference between the two.
C和c++中的规则是一样的,两者之间没有区别。
What does the C++ Standard (n3797) say?
c++标准(n3797)说了什么?
5.2.2p1
Function call[expr.call]
5.2.2p1函数调用(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.
函数调用是后缀表达式,后跟圆括号,括号中可能包含空的、逗号分隔的initializer-子句列表,它们构成函数的参数。
The postfix expression shall have function type or pointer to function type.
后缀表达式应该具有函数类型或指向函数类型的指针。
What does the C standard (n1570) say?
C标准(n1570)说什么?
6.3.2.1p4
Lvalues, arrays, and function designators6.3.2.1p4 lvalue、数组和函数指示器
A 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".函数指示器是具有函数类型的表达式。除了作为sizeof运算符、_Alignof运算符或一元&运算符的操作数时,具有“函数返回类型”的函数指示器被转换为具有“函数返回类型指针”的表达式。
6.5.2.2p1
Function calls6.5.2.2p1函数调用
The 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.表示被调用函数的表达式应该具有返回void或返回除数组类型之外的完整对象类型的类型指针。
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.
在C语言中,隐式函数-指针转换在调用函数时总是适用,而c++则声明“后缀表达式”可以是指针,也可以是函数类型的变量。
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.
然而;您的问题是,如果在c++和C之间通过指针调用函数的两种方式不同,那么答案是:不,(1)、(2)和(3)之间没有区别,这两种语言之间也没有区别。
(*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.
注意:在调用函数时,要注意(*fptr)(…)和fptr(…)之间没有区别,但是在其他上下文中它们可能有很大的不同。
#2
3
How are the rules? Do they differ in C and C++?
规则怎么样?C和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.
但是,您可能仍然希望使用星号标记来强调您正在通过函数指针调用一个函数。这也可能是本文中代码片段作者的原因。