美文网首页Programmer
函数指针算法库

函数指针算法库

作者: github_lincy | 来源:发表于2019-01-08 10:30 被阅读0次

函数指针, 函数指针数组声明

typedef  int (*fun_t)(void*, ....)
typedef  int (*fun_t[])(void*, ...)

通过参数指定回调函数,代码示例

typedef int (*fun0_t)(void);
typedef int (*fun1_t)(int *);
typedef int (*fun2_t)(int *, int *);
typedef int (*fun3_t)(int *, int *, int *);
typedef int (*fun4_t)(int *, int *, int *, int *);
typedef int (*fun5_t)(int *, int *, int *, int *, int *);
typedef int (*fun6_t)(int *, int *, int *, int *, int *, int *);
typedef int (*fun7_t)(int *, int *, int *, int *, int *, int *, int *);
typedef int (*fun8_t)(int *, int *, int *, int *, int *, int *, int *, int *);
typedef int (*fun9_t)(int *, int *, int *, int *, int *, int *, int *, int *, int *);
typedef int (*fun10_t)(int *, int *, int *, int *, int *, int *, int *, int *, int *, int *);

typedef int (*call_fun_t)(int *arg[], fun_t fun);

static int call_fun0(int *arg[], fun_t fun)
{
    fun0_t fun0;
    int rv;

    fun0 = (fun0_t)fun;
    rv = fun0();

    return rv;
}

static int call_fun1(int *arg[], fun_t fun)
{
    fun1_t fun1;
    int rv;

    fun1 = (fun1_t)fun;
    rv = fun1(arg[0]);

    return rv;
}

static int call_fun2(int *arg[], fun_t fun)
{
    fun2_t fun2;
    int rv;

    fun2 = (fun2_t)fun;
    rv = fun2(arg[0], arg[1]);

    return rv;
}

static int call_fun3(int *arg[], fun_t fun)
{
    fun3_t fun3;
    int rv;

    fun3 = (fun3_t)fun;
    rv = fun3(arg[0], arg[1], arg[2]);

    return rv;
}

static int call_fun4(int *arg[], fun_t fun)
{
    fun4_t fun4;
    int rv;

    fun4 = (fun4_t)fun;
    rv = fun4(arg[0], arg[1], arg[2], arg[3]);

    return rv;
}

static int call_fun5(int *arg[], fun_t fun)
{
    fun5_t fun5;
    int rv;

    fun5 = (fun5_t)fun;
    rv = fun5(arg[0], arg[1], arg[2], arg[3], arg[4]);

    return rv;
}

static int call_fun6(int *arg[], fun_t fun)
{
    fun6_t fun6;
    int rv;

    fun6 = (fun6_t)fun;
    rv = fun6(arg[0], arg[1], arg[2], arg[3], arg[4], arg[5]);

    return rv;
}

static int call_fun7(int *arg[], fun_t fun)
{
    fun7_t fun7;
    int rv;

    fun7 = (fun7_t)fun;
    rv = fun7(arg[0], arg[1], arg[2], arg[3], arg[4], arg[5], arg[6]);

    return rv;
}

static int call_fun8(int *arg[], fun_t fun)
{
    fun8_t fun8;
    int rv;

    fun8 = (fun8_t)fun;
    rv = fun8(arg[0], arg[1], arg[2], arg[3], arg[4], arg[5], arg[6], arg[7]);

    return rv;
}

static int call_fun9(int *arg[], fun_t fun)
{
    fun9_t fun9;
    int rv;

    fun9 = (fun9_t)fun;
    rv = fun9(arg[0], arg[1], arg[2], arg[3], arg[4], arg[5], arg[6], arg[7], arg[8]);

    return rv;
}

static int call_fun10(int *arg[], fun_t fun)
{
    fun10_t fun10;
    int rv;

    fun10 = (fun10_t)fun;
    rv = fun10(arg[0], arg[1], arg[2], arg[3], arg[4], arg[5], arg[6], arg[7], arg[8], arg[9]);

    return rv;
}

static call_fun_t g_call[] = {
    call_fun0, call_fun1, call_fun2, call_fun3, call_fun4, call_fun5, call_fun6, call_fun7, call_fun8, call_fun9, call_fun10
};

int callfun(int argc, int * argv[], fun_t fun)
{
    int rv;

    rv = g_call[argc](argv, fun);
  
    return rv;
}

算法库代码说明

  • argc:参数个数
  • argv:回调函数的序列化参数列表
  • fun: 通过参数个数指定回调函数

相关文章

  • 函数指针算法库

    函数指针, 函数指针数组声明 通过参数指定回调函数,代码示例 算法库代码说明 argc:参数个数 argv:回调函...

  • 函数和指针

    函数指针: 指向函数的指针(是指针)指针函数:返回值是指针的函数(是函数)

  • 函数指针

    概念: 指针函数, 函数指针, 指针数组, 数组指针, 指向数组的指针, 指向函数指针数组的指针。

  • 指针

    一. 指针指向的是对象的地址//函数指针:指针指向函数//指针函数:函数返回指针 二.

  • C:函数指针的坑

    关于该死的函数指针和指针函数 先来个目录 常量指针、指针常量 数组指针、指针数组 函数指针、指针函数 1、先看第一...

  • C语言基础知识点

    函数指针与回调函数 1、函数指针:函数指针是指向函数的指针变量,以下实例声明了函数指针变量 p,指向函数 max:...

  • NDK启航篇——C语言基础(函数指针)

    昨天介绍了指针类型、空指针、指针运算,今天来写一下函数指针 函数指针 函数指针的定义:函数的返回值类型(函数指针的...

  • C语言基础---函数指针和回调函数

    版权声明:本文为小斑马伟原创文章,转载请注明出处!函数指针:函数指针 是指向函数的指针。指针函数:指针函数 函数...

  • Redis

    1.指针函数与函数指针 指针函数本质是指针,其返回值是指针。如 float *fun(); 函数指针,本质是指针。...

  • C++:函数指针 & 返回函数指针的函数

    函数指针 & 返回函数指针的函数 一、函数指针的声明和使用 声明一个函数指针,给它赋值并调用它指向的函数 函数指针...

网友评论

    本文标题:函数指针算法库

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