iOS学习之C语言函数指针
通过函数名调用函数:
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语言函数指针的更多相关文章
- IOS学习笔记07---C语言函数-printf函数
IOS学习笔记07---C语言函数-printf函数 0 7.C语言5-printf函数 ------------------------- ----------------------------- ...
- IOS学习笔记06---C语言函数
IOS学习笔记06---C语言函数 -------------------------------------------- qq交流群:创梦技术交流群:251572072 ...
- iOS学习之C语言函数
一.函数的定义 返回值类型 函数名(参数类型 参数名, ...) { 功能语句; return 返回值; } 按照返回值和参数划分: 第一种: 无返回值 无参 void sayHello() { pr ...
- C#委托与C语言函数指针及函数指针数组
C#委托与C语言函数指针及函数指针数组 在使用C#时总会为委托而感到疑惑,但现在总新温习了一遍C语言后,才真正理解的委托. 其实委托就类似于C/C++里的函数指针,在函数传参时传递的是函数指针,在调用 ...
- C语言函数指针 和 OC-Block
C语言函数指针 和 OC-Block 一. C语言函数指针 关于函数指针的知识详细可参考:http://www.cnblogs.com/mjios/archive/2013/03/19/2967037 ...
- C语言函数指针基础
本文写的非常详细,因为我想为初学者建立一个意识模型,来帮助他们理解函数指针的语法和基础.如果你不讨厌事无巨细,请尽情阅读吧. 函数指针虽然在语法上让人有些迷惑,但不失为一种有趣而强大的工具.本文将从C ...
- “对外部(局部)变量的访问”是C语言函数指针的最大弱点
1.“对外部(局部)变量的访问”是C语言函数指针的最大弱点 . #include <stdio.h> #include <stdlib.h> /* 结构体定义 */ struc ...
- iOS学习09C语言函数指针
本次主要学习和理解函数指针 1.函数指针 void printValue(int number) { printf("number = %d\n", number); } int ...
- c语言函数指针的理解与使用(学习)
1.函数指针的定义 顾名思义,函数指针就是函数的指针.它是一个指针,指向一个函数.看例子: 1 2 3 A) char * (*fun1)(char * p1,char * p2); B) char ...
随机推荐
- android + javascript 相互通信实例分析
1. AndroidManifest.xml中必须使用许可 "android.permission.INTERNET", 否则会出Web page not available错误 ...
- Android基础总结(2)——活动Activity
1.什么是活动(Activity) 活动(Activity)是一种可以包含用户界面的组件,主要用于和用户进行交互.一个应用程序中可以包含零个或多个活动,但不包含任何活动的应用程序很少见. 2.怎么使用 ...
- 深入了解Qt(二)之元对象系统(Meta-Object System)
深入了解Qt主要内容来源于Inside Qt系列,本文做了部分删改,以便于理解.在此向原作者表示感谢! 在Qt Meta Object System-元对象系统这篇文章中,从底层实现的源码剖析了元对象 ...
- select修改原生样式组件
<div class="select"> <select class="" name=""> <option ...
- Linux下对于inode的理解
0x01 什么是inode 文件存储在硬盘上,硬盘的最小存储单位叫做“扇区”(Sector),每个扇区储存512字节: 操作系统读取硬盘时,不会一个个扇区地读取,这样效率低,而是一次性连续读取多个扇区 ...
- 用Zend Studio12 导入在workspace中的项目
第一步: 文件(file)->新建(NEW)->其他(other) 第二步: 在打开的对话框里选择 常规(Genneral)->项目(Project) 第三步: 在打开的对话框输入项 ...
- javaSE第二十四天
第二十四天 363 1:多线程(理解) 363 (1)JDK5以后的Lock锁 363 A:定义 363 B:方法: 364 C:具体应用(以售票程序为例) 364 ...
- jquery.form.js实现将form提交转为ajax方式提交的使用方法
本文实例讲述了jquery.form.js实现将form提交转为ajax方式提交的方法.分享给大家供大家参考.具体分析如下: 这个框架集合form提交.验证.上传的功能. 这个框架必须和jquery完 ...
- JavaScript模块化---AMD规范
JavaSript模块化 在了解AMD,CMD规范前,还是需要先来简单地了解下什么是模块化,模块化开发? 模块化是指在解决某一个复杂问题或者一系列的杂糅问题时,依照一种分类的思维把问 题进行系 ...
- Vue.js学习 Item12 – 内部响应式原理探究
深入响应式原理 大部分的基础内容我们已经讲到了,现在讲点底层内容.Vue.js 最显著的一个功能是响应系统 —— 模型只是普通对象,修改它则更新视图.这让状态管理非常简单且直观,不过理解它的原理也很重 ...