通过函数名调用函数:

  int max = maxValue(4, 5);
  printf("max = %d\n", max);
    
  函数类型:int (int, int)

  1.定义函数指针

  int *p = NULL;

  函数类型:int (int, int)

  函数指针的类型:int (*)(int, int)

  p是函数指针变量名

  int (*p)(int, int) = NULL;

  2.给函数指针赋值(使用函数首地址)

  函数存放在代码区,函数名是函数存储空间的首地址

  p = maxValue;

  3.通过函数指针调用函数和通过函数名调用函数一样。

  动态排序:

  使用回调函数,提高代码的复用性,提高代码的可修改性(当需求有变化时,可以快速简单的修改)

  

 Function.h
 struct student {

     ];  // 姓名
     int age;        // 年龄
     double score;   // 成绩
     int num;        // 学号

 };
 typedef
 // 打印所有学生的信息
 void printArray(Student stuArray[], int count);

 // 比较两个学生的年龄
 BOOL compareStuAge(Student stu1, Student stu2);

 // 比较两个学生的姓名
 BOOL compareStuName(Student stu1, Student stu2);

 // 比较两个学生的成绩
 BOOL compareStuScore(Student stu1, Student stu2);

 // 比价两个学生的学号
 BOOL compareStuNum(Student stu1, Student stu2);

 typedef BOOL (*FUNC)(Student, Student);

 // 排序函数
 void sortStudent(Student *stu, int count, FUNC p);
 Function.m

 // 比较两个学生的年龄
 BOOL compareStuAge(Student stu1, Student stu2) {
     return stu1.age > stu2.age;
 }

 // 比较两个学生的姓名
 BOOL compareStuName(Student stu1, Student stu2) {
     ;
 }

 // 比较两个学生的成绩
 BOOL compareStuScore(Student stu1, Student stu2) {
     return stu1.score < stu2.score;
 }

 // 比价两个学生的学号
 BOOL compareStuNum(Student stu1, Student stu2) {
     return stu1.num > stu2.num;
 }

 // 排序函数
 void sortStudent(Student *stu, int count, FUNC p) {

     ; i < count - ; i++) {
         ; j < count -  - i; j++) {
             ])) {
                 Student temp = stu[j];
                 stu[j] = stu[j + ];
                 stu[j + ] = temp;
             }
         }
     }
 }
 main.m
 int main(int argc, const char * argv[]) {

     Student stu1 = {, };
     Student stu2 = {, , };
     Student stu3 = {, -, };
     Student stu4 = {, , };
     Student stu5 = {, , };

     Student stuArray[] = {stu1, stu2, stu3, stu4, stu5};
     int count = sizeof(stuArray) / sizeof(Student);
 // 动态排序
     sortStudent(stuArray, count, compareStuNum);
     printArray(stuArray, count);
     sortStudent(stuArray, count, compareStuAge);
     printArray(stuArray, count);
     sortStudent(stuArray, count, compareStuName);
     printArray(stuArray, count);
     sortStudent(stuArray, count, compareStuScore);
     printArray(stuArray, count);

iOS学习之C语言函数指针的更多相关文章

  1. IOS学习笔记07---C语言函数-printf函数

    IOS学习笔记07---C语言函数-printf函数 0 7.C语言5-printf函数 ------------------------- ----------------------------- ...

  2. IOS学习笔记06---C语言函数

    IOS学习笔记06---C语言函数 --------------------------------------------  qq交流群:创梦技术交流群:251572072              ...

  3. iOS学习之C语言函数

    一.函数的定义 返回值类型 函数名(参数类型 参数名, ...) { 功能语句; return 返回值; } 按照返回值和参数划分: 第一种: 无返回值 无参 void sayHello() { pr ...

  4. C#委托与C语言函数指针及函数指针数组

    C#委托与C语言函数指针及函数指针数组 在使用C#时总会为委托而感到疑惑,但现在总新温习了一遍C语言后,才真正理解的委托. 其实委托就类似于C/C++里的函数指针,在函数传参时传递的是函数指针,在调用 ...

  5. C语言函数指针 和 OC-Block

    C语言函数指针 和 OC-Block 一. C语言函数指针 关于函数指针的知识详细可参考:http://www.cnblogs.com/mjios/archive/2013/03/19/2967037 ...

  6. C语言函数指针基础

    本文写的非常详细,因为我想为初学者建立一个意识模型,来帮助他们理解函数指针的语法和基础.如果你不讨厌事无巨细,请尽情阅读吧. 函数指针虽然在语法上让人有些迷惑,但不失为一种有趣而强大的工具.本文将从C ...

  7. “对外部(局部)变量的访问”是C语言函数指针的最大弱点

    1.“对外部(局部)变量的访问”是C语言函数指针的最大弱点 . #include <stdio.h> #include <stdlib.h> /* 结构体定义 */ struc ...

  8. iOS学习09C语言函数指针

    本次主要学习和理解函数指针 1.函数指针 void printValue(int number) { printf("number = %d\n", number); } int ...

  9. c语言函数指针的理解与使用(学习)

    1.函数指针的定义 顾名思义,函数指针就是函数的指针.它是一个指针,指向一个函数.看例子: 1 2 3 A) char * (*fun1)(char * p1,char * p2); B) char  ...

随机推荐

  1. hibernate进阶--一对多映射配置

    hibernate作为一款优秀的ORM框架,广受大家喜爱,也被Java社区公认为持久层的首选.虽然jdbc为Java数据库操 作带来了诸多便利,但是并没有统一SQL语句的写法,而且具体操作数据库的代码 ...

  2. jetty简介

    Jetty 是一个开源的servlet容器,它为基于Java的web容器,例如JSP和servlet提供运行环境.Jetty是使用Java语言编写的,它的API以一组JAR包的形式发布.开发人员可以将 ...

  3. VS2010插件及快捷键设置

    几个常用的Visual Studio插件,番茄助手以及如下的插件,具体作用可用通过Google自行获取. 安装番茄助手后,可用在源文件和头文件中快速切换.但为了更方便使用,建议设置快捷键. vs201 ...

  4. MyBatis学习系列一之环境搭建

    目录 MyBatis学习系列一之环境搭建 MyBatis学习系列二——增删改查 MyBatis学习系列三——结合Spring 学习一个新的知识,首先做一个简单的例子使用一下,然后再逐步深入.MyBat ...

  5. import static与import的区别

    import static(静态导入)是JDK1.5中的新特性,一般我们导入一个类都用 import com.....ClassName;而静态导入是这样:import static com..... ...

  6. IIS报错 未将对象引用设置到对象的实例。

    在vs中运行正常的项目 ,发布到IIS总是提示 未将对象引用设置到对象的实例. 运行静态页面 html正常,只是打开.aspx页面的时候报错,在确保了数据库,配置,权限均正常的情况下. 错误原因:先安 ...

  7. 三、第一个Struts2应用案例(编码步骤)

    第一个Struts2应用案例(编码步骤) 编写2个jsp hello.jsp <body>     <a href="${pageContext.request.conte ...

  8. C puzzles详解

    题目:http://www.gowrikumar.com/c/ 参考:http://wangcong.org/blog/archives/291 http://www.cppblog.com/smag ...

  9. C puzzles详解【38-45题】

    第三十八题 What is the bug in the following program? #include <stdlib.h> #include <stdio.h> # ...

  10. bea weblogic workshop中文乱码

    重装系统后,weblogic 8.1 workshop中的中文字体是乱码. 可通过菜单中的 Tools -> IDE Properties -> Display, 在Window font ...