C 函数指针 转移表

2017-11-28  本文已影响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;
}
上一篇下一篇

猜你喜欢

热点阅读