码迷,mamicode.com
首页 > 其他好文 > 详细

函数指针、回调、动态排序、返回函数指针

时间:2014-09-14 23:25:17      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:style   color   io   os   ar   for   文件   div   sp   

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;
}

函数指针、回调、动态排序、返回函数指针

标签:style   color   io   os   ar   for   文件   div   sp   

原文地址:http://www.cnblogs.com/Alling/p/3971875.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!