zoukankan      html  css  js  c++  java
  • 函数指针、回调、动态排序、返回函数指针

    1、函数指针定义及初始化:

    int (*p)(int,int)=NULL;
     
    p=maxValue;//将函数maxValue的地址传给p
    (*p)=maxValue;
     
    2、函数回调:
    int getValue(int a,int b,int (*p)(int,int));//函数指针作为getValue的参数
    函数指针指向函数可变
     
    3、动态排序(排序条件多变)
    将决定排序方式的条件封装成函数,然后再回调
    typedef BOOL (*P_Fun)(int,int);//头文件#include <stdbool.h>
     
    void sortArray(int *arr,int count,P_Fun p);
     
    4、函数返回值是函数指针
    P_Fun getFunctionByName(char *name)
    通过功能名称查找对应的函数
    NameFunctionList list[]={
        {"max",maxValue},
        {"min",minValue},
        {"avg",avgValue},
        {"sum",sumValue},
        {"mul",mulValue
    }

    };
    P_FUN getFunctionByName(char *name)
    {
        for (int i=0; i<(sizeof(list)/sizeof(list[0])); i++)
        {
            if (!strcmp(list[i].name, name))
            {
                return list[i].function
    ;
            }

        }
        return maxValue;
    }
  • 相关阅读:
    【Go】http server 性能测试
    【go】基础
    【Git】gitcongfig 增删改查
    【go】sdk + idea-plugin 开发工具安装
    【Ibatis】总结各种使用技巧
    【nodejs】jade模板入门
    各语言技术列表
    【nodejs】 npm 注意事项
    PAT-l3-002堆栈
    统计相似字符串
  • 原文地址:https://www.cnblogs.com/Alling/p/3971875.html
Copyright © 2011-2022 走看看