C 函数指针 转移表

函数指针

转移表

/***********************************************
*
* \Filename    funcptr_shift_tbl.c
*
* \Author      XXX from Xidian
*              [email protected]
*
* \Desc
*
* \Modified History
*
*
* \CreateTime  2017-11-28 20:24:58
*
***********************************************/
 
#include 
#include 
#include 

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 函数指针 转移表)