一,举例应用

  在ACM比赛中常使用 stdlib.h 中自带的 qsort 函数,是教科书式的函数指针应用示范。

#include <stdio.h>
#include <stdlib.h> int comp(const void*a, const void*b)
{
return *(int*)a - *(int*)b;
} int main()
{
int n = 5;
int *array = (int*)malloc(n*sizeof(int));
int i = 0;
for (; i<n; i++)
{
*(array + i) = 10 - i;
}
qsort(array, n, sizeof(int), comp);
for (i = 0; i<n; i++)
{
printf("%d\t", array[i]);
}
return 0;
}

二,总结

  函数指针变量通常的用途之一,是把指针作为参数传递到其他函数。

  调用函数指针的例子:

int locateElem(LinearList *pL, ElemType e, int (*compare)(ElemType*, ElemType*))

  其中 compare 是函数指针名,(ElemType*, ElemType*)是函数指针的参数。

  调用函数指针的例子:

if ((*compare)(&e1, &e2) == 0)
return i;

  参数 &e1 和 &e2 是两个类型为 ElemType* 的变量。

函数指针(pointer to function)——qsort函数应用实例的更多相关文章

  1. 指针函数(Pointer Function)和函数指针(Pointer to Function或Function Pointer)

    一.指针函数 1.解释:指针函数很好理解:简单来说,就是一个返回指针的函数,本质是一个函数.如: int fun(int x,int y);    //这是一个普通函数的声明,返回值是一个int类型, ...

  2. typedef 函数指针 数组 std::function

    1.整型指针 typedef int* PINT;或typedef int *PINT; 2.结构体 typedef struct { double data;}DATA,  *PDATA;  //D ...

  3. C语言:判断字符串是否为回文,-函数fun将单向链表结点数据域为偶数的值累加起来。-用函数指针指向要调用的函数,并进行调用。

    //函数fun功能:用函数指针指向要调用的函数,并进行调用. #include <stdio.h> double f1(double x) { return x*x; } double f ...

  4. 类非静态成员的函数指针 的使用 Function pointer of a non-static member function of a class

    you can get the pointer of the method, but it has to be called with an object typedef void (T::*Meth ...

  5. 函数指针和qsort函数

    1.函数指针的形式: 函数指针:int (*funcP) (int *a, int *b) 表示定义了一个funcP函数指针,指向了返回值为int类型,参数为int* 和int* 的函数 使用方式: ...

  6. c++11 符号修饰与函数签名、函数指针、匿名函数、仿函数、std::function与std::bind

    一.符号修饰与函数签名 1.符号修饰 编译器将c++源代码编译成目标文件时,用函数签名的信息对函数名进行改编,形成修饰名.GCC的C++符号修饰方法如下: 1)所有符号都以_z开头 2)名字空间的名字 ...

  7. 虚函数指针sizeof不为sizeof(void*)

    ref:http://bbs.csdn.net/topics/360249561 一个继承了两个虚基类又增加了自己的一个虚函数pif的类,sizeof(指向pif的指针)竟然是8(X86).我是从这里 ...

  8. 成员函数指针与高效C++委托 (delegate)

    下载实例源代码 - 18.5 Kb 下载开发包库文件 - 18.6 Kb 概要 很遗憾, C++ 标准中没能提供面向对象的函数指针. 面向对象的函数指针也被称为闭包(closures) 或委托(del ...

  9. 成员函数指针与高性能C++委托

    1 引子 标准C++中没有真正的面向对象的函数指针.这一点对C++来说是不幸的,因为面向对象的指针(也叫做“闭包(closure)”或“委托(delegate)”)在一些语言中已经证明了它宝贵的价值. ...

  10. [转]成员函数指针与高性能的C++委托

    原文(作者:Don Clugston):Member Function Pointers and the Fastest Possible C++ Delegates 译文(作者:周翔): 成员函数指 ...

随机推荐

  1. CSU1129 送货到家 【状压dp】

    哈哈发现这道题竟然没有题解,于是我决定写一份! 状压dp 题目: 懒惰的巫女Reimu因为各种原因在香霖堂的店主Rinnosuke那儿欠下了很多债,于是乎只好靠帮他在幻想乡中送货来偿还掉微不足道的一小 ...

  2. oracle11gR2 win7_32位客户端连接虚拟机中oracle11gR2 win7_32位服务器方法

    改写服务器中的监听文件(listener.ora和tnsnames.ora) “ora-12541:TNS:无监听程序”问题的解决 ora-12541:TNS:无监听程序,出现这种错误的时候,可以尝试 ...

  3. Spark Streaming 在数据平台日志解析功能的应用

    https://mp.weixin.qq.com/s/bGXhC9hvDj4lzK7wYYHGDg 目前,我们使用Filebeat监控日志产生的目录,收集产生的日志,打到logstash集群,接入ka ...

  4. referrer privacy hotlinking

    https://en.wikipedia.org/wiki/HTTP_referer https://zh.wikipedia.org/wiki/HTTP参照位址 inline linking, of ...

  5. Javascript异步执行时要小心的变量作用域

    function asyncFunction(callback){ setTimeout(function(){ callback() },200); } var color = 'blue'; // ...

  6. getContextPath、getServletPath、getRequestURI、request.getRealPath的区别

    1 区别 假定你的web application 名称为news,你在浏览器中输入请求路径: http://localhost:8080/news/main/list.jsp 1.1 System.o ...

  7. pycharm中python模板代码自动生成

    # -*- coding: utf-8 -*- """ ------------------------------------------------- File Na ...

  8. 鼠标滑动到指定位置时div固定在头部

    $(function(){ $(window).scroll(function () {            if ($(window).scrollTop() > 253) {       ...

  9. docker的安装,使用

    1:安装:https://blog.csdn.net/yx_222/article/details/80936757 sudo apt-get install docker.io 2:  使用dock ...

  10. hdu1166敌兵布阵&&hdu1754I Hate It(线段树入门)

    单点更新是最最基础的线段树,只更新叶子节点,然后把信息用pushup这个函数更新上来. http://acm.hdu.edu.cn/showproblem.php?pid=1166 update单点更 ...