问题描述
这两个声明有什么区别:
What is the difference between these 2 declaration:
int operate(int (*func)(int, int), int a, int b){
return (*func)(a, b);
}
和
int operate(int func(int, int), int a, int b){
return func(a, b);
}
这两个似乎也等效:operate(sum, 1, 1)
和operate(&sum, 1, 1)
These two also seems to be equivalent: operate(sum, 1, 1)
and operate(&sum, 1, 1)
如果我将函数sum
作为2个数字的函数传递给func
,则结果仍然相同.为什么?
If I pass function sum
as a function of 2 numbers in the place of func
, the result are still the same. Why?
推荐答案
§6.7.5.3/8:
§6.7.5.3/8:
换句话说,两个函数声明是相同的.
In other words, the two function declarations are identical.
就函数调用而言,第6.5.2.2/3节:
As far as the function call goes, §6.5.2.2/3:
由于func(a, b);
和(*func)(a, b)
都是后缀表达式,后跟括号,因此它们都是函数调用.由于func
和(*func)
都指定相同的功能,因此它们都调用相同的功能.
Since both func(a, b);
and (*func)(a, b)
are postfix expressions followed by parentheses, they're both function calls. Since func
and (*func)
both designate the same function, they both call the same function.
这篇关于C函数指针混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!