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 ...
随机推荐
- mybatis-generator 代码自动生成工具
今天来介绍下怎么用mybatis-gennerator插件自动生成mybatis所需要的dao.bean.mapper xml文件,这样我们可以节省一部分精力,把精力放在业务逻辑上. 之前看过很多文章 ...
- 如何初始化一个iOS原生地图
/** 初始化一个mapView 需导入 #import <MapKit/MapKit.h> - returns: 返回一个mapView对象 */ mapView = [[MKMapV ...
- Servlet小试
Java响应Http请求: 1.创建项目 因为对Java环境不是很熟悉,第一步卡住了好长时间, javax怎么引用, 在Java EE 5 Libraries中的javaee.jar中,项目中怎么引用 ...
- 著名的二分查找的BUG
int binarySearch(int a[], int key, int length) { int low = 0; int high = length - 1; while (low < ...
- UIButton设置imgae图片自适应button的大小且不变形
在某些情况下,我们使用的UIButton的背景图片不一定就是标准的尺寸,有时会偏大,那么怎么办? 这个比较简单直接设置 : self.imageView.contentMode = UIView ...
- Mysql 自定义随机字符串
前几天在开发一个系统,需要用到随机字符串,但是mysql的库函数有没有直接提供,就简单的利用现有的函数东拼西凑出随机字符串来.下面简单的说下实现当时. 1.简单粗暴. select ..., subs ...
- iConvert Icons 图标转换生成利器,支持Windows, Mac OS X, Linux, iOS,和Android等系统
这是一款在线图标转换工具,生成的图标支持Windows, Mac OS X, Linux, iOS, 和 Android等主流系统. 可以上传图标文件转化成另一个平台下的图标文件,例如将windows ...
- HTTP状态
HTTP状态码 当浏览者访问一个网页时,浏览者的浏览器会向网页所在服务器发出请求.当浏览器接收并显示网页前,此网页所在的服务器会返回一个包含HTTP状态码的信息头(server header)用以响应 ...
- C#winform在textbox插入内容换行
要让一个TextBox显示多行文本就得把它的Multiline属性设置为true,可是如果你是要把TextBox的Text属性设置多行文本时可能会遇到点麻烦,也许你会想到直接加一个换行符"\ ...
- C# 运行时编辑 节点重命名
方法一: ; bool nodeChanged = false; //右键点击,就进入修改状态 private void treeView1_NodeMouseClick(object sender, ...