C函数指针混乱 [英] C function pointer confusion

查看:73
本文介绍了C函数指针混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两个声明有什么区别:

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:

将参数声明为函数返回类型"声明调整为指向函数返回类型的指针",如6.3.2.1.所示.

A declaration of a parameter as ‘‘function returning type’’ shall be adjusted to ‘‘pointer to function returning type’’, as in 6.3.2.1.

换句话说,两个函数声明是相同的.

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:

后缀表达式,后跟括号(),其中可能为空,以逗号分隔 表达式列表是一个函数调用.后缀表达式表示被调用的函数.

A postfix expression followed by parentheses () containing a possibly empty, comma-separated list of expressions is a function call. The postfix expression denotes the called function.

由于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函数指针混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆