函数指针:为什么(* FPTR)(6,7)同(FPTR)(6,7),如果函数添加(INT,INT) [英] function pointer : why is (*fptr)(6,7) same as (fptr)(6,7) , if the function is add(int , int )

查看:165
本文介绍了函数指针:为什么(* FPTR)(6,7)同(FPTR)(6,7),如果函数添加(INT,INT)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

函数指针

为什么(* FPTR)(int类型的,INT B)是一样的(FPTR)(INT A,INT B)如果函数指针被分配到补充功能?

  INT(* FPTR)(INT,INT)=增加;
同时添加(int类型的,INT B)返回两个数字的总和。


解决方案

说明

C没有功能的目标,所以它是没有意义的取消引用函数指针。因此,当一个函数指针被废弃,它是(有一些例外)立即转身回函数指针。

参考

n1570 (最后的公开草案当前的C标准):


  

6.5.3.2地址和间接运营商


  
  <醇开始=4>
  
  • 单目 * 运营商表示间接。如果操作数指向一个函数,结果是一个功能代号; [...]

  •   

    ...


      

    6.3.2.1左值,数组和功能指示器


      
      <醇开始=4>
      
  • A 功能代号是一个具有函数型的前pression。除非它是的sizeof 运算符的操作数,则 _Alignof 运营商,或一元 &安培; 运营商,类型'函数返回类型'功能标志被转换为具有类型的前pression'的指针函数返回类型

  •   

    In function pointers,

    Why is (*fptr)(int a, int b) is same as (fptr)(int a, int b) if the function pointer is assigned to add function?

    int (*fptr)(int ,int) = add; 
    while add(int a , int b) returns sum of two numbers.    
    

    解决方案

    Explanation

    C doesn't have function objects, so it makes no sense to dereference a function pointer. Therefore, when a function pointer is dereferenced, it is (with some exceptions) immediately turned back into a pointer to function.

    References

    n1570 (the final public draft of the current C standard):

    6.5.3.2 Address and indirection operators

    1. The unary * operator denotes indirection. If the operand points to a function, the result is a function designator; [...]

    ...

    6.3.2.1 Lvalues, arrays, and function designators

    1. 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’’.

    这篇关于函数指针:为什么(* FPTR)(6,7)同(FPTR)(6,7),如果函数添加(INT,INT)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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