美文网首页
C 函数指针 转移表

C 函数指针 转移表

作者: 蓝雪冬荷 | 来源:发表于2017-11-28 21:04 被阅读39次

    函数指针

    转移表

    /***********************************************
    *
    * \Filename    funcptr_shift_tbl.c
    *
    * \Author      XXX from Xidian
    *              2550572497@qq.com
    *
    * \Desc
    *
    * \Modified History
    *
    *
    * \CreateTime  2017-11-28 20:24:58
    *
    ***********************************************/
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    double add_(double x, double y);
    double sub_(double x, double y);
    double mul_(double x, double y);
    double div_(double x, double y);
    
    double (*oper_func[])(double x, double y) = {add_, sub_, mul_, div_};
    
    int main(int argc, char* argv[])
    {
        double x = 3.1, y = 3.2;
        for(int i=0; i<4; i++) {
            oper_func[i](x, y);
        }
        
        return 0;
    }
    
    
    double add_(double x, double y) {
        printf("x+y = %lf\n", x+y);
        return x+y;
    }
    
    double sub_(double x, double y) {
        printf("x-y = %lf\n", x-y);
        return x-y;
    }
    
    double mul_(double x, double y) {
        printf("x*y = %lf\n", x*y);
        return x*y;
    }
    
    double div_(double x, double y) {
        printf("x/y = %lf\n", x/y);
        return x/y;
    }
    

    相关文章

      网友评论

          本文标题:C 函数指针 转移表

          本文链接:https://www.haomeiwen.com/subject/lnaebxtx.html