本次主要学习和理解函数指针

1.函数指针

 void printValue(int number) {
printf("number = %d\n", number);
}
int main(int argc, const char * argv[]) {
void (*p1)(int) = NULL;
p1 = printValue;
p1();
}

 1> 定义

  代码第5行

  函数类型:int (int, int)

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

  p是函数指针变量名

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

  代码第6行

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

 3> 通过函数指针调用函数

  代码第7行

  与通过函数名调用函数是一样的

 练习:定义两个函数,一个求两个数的 最大值,一个求两个数的 和,从控制台输入maxValue或sum分别求3和5的最大值或和(提示:定义一个函数指针,根据输入内容指向不同函数,最后一次 调用完成)。

Func.h

 #import <Foundation/Foundation.h>

 int maxValue(int a, int b);
int sum(int a, int b);

Func.m

 #import "Func.h"

 int maxValue(int a, int b) {
return a > b ? a : b;
} int sum(int a, int b) {
return a + b;
}

main.m

 #import <Foundation/Foundation.h>

 #import "Func.h"

 int main(int argc, const char * argv[]) {
char str[] = {};
int (*p)(int, int) = NULL; printf("输入maxValue或sum:\n");
scanf("%s", str);
if (strcmp("maxValue", str) == ) {
p = maxValue;
printf("最大值:");
} else if (strcmp("sum", str) == ) {
p = sum;
printf("和:");
} else {
printf("输入有误,函数名不存在!\n");
}
// 安全判断
if (p != NULL) {
printf("%d\n", p(, ));
}
return ;
}

第20行代码是一个安全判断,是程序更加健壮。

2、调用回调函数

回调函数就是 函数指针作为函数参数(一般是函数的最后一个参数)

练习:写一函数查找成绩90分以上的学员,使用回调函数在姓名后加”高富帅”。
     提示:定义两个函数
     1、void addStr(char *name);
     2、void findStudentByScore(Student *stus, int count, void(*p)(char *))

Func.h

 #import <Foundation/Foundation.h>

 struct student {
char name[];
int age;
int score;
};
typedef struct student Student; void addStr(char *name);
void findStudentByScore(Student *stus, int count, void(*p)(char *));
void printArray(Student *stus, int count);

func.m

 void addStr(char *name) {
strcat(name, "高富帅");
}
void findStudentByScore(Student *stus, int count, void(*p)(char *)) {
for (int i = ; i < count; i++) {
if (stus[i].score >= ) {
p(stus[i].name); }
}
} void printArray(Student *stus, int count) {
for (int i = ; i < count; i++) {
printf("name = %s,age = %d,score = %d\n", stus[i].name, stus[i].age, stus[i].score);
}
}

main.m

 #import <Foundation/Foundation.h>

 #import "Func.h"

 int main(int argc, const char * argv[]) {

     Student stu1 = {"张三", , };
Student stu2 = {"张四", , };
Student stu3 = {"张五", , };
Student stus[] = {stu1, stu2, stu3};
findStudentByScore(stus, , addStr);
printArray(stus, ); return ;
}

3、动态排序

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

Function.h

 #import <Foundation/Foundation.h>

 struct student {
char name[]; // 姓名
int age; // 年龄
double score; // 成绩
int num; // 学号
};
typedef struct student Student; // 函数指针重定义,重定义的名字为 * 后面的名字
typedef BOOL (*Function)(Student, Student); // 打印数组元素
void printArray(Student *stu, int count); // 比较两个学生的年龄
BOOL compareStuAgeAsc(Student stu1, Student stu2); // 比较两个学生的姓名
BOOL compareStuNameAsc(Student stu1, Student stu2); // 比较两个学生的成绩
BOOL compareStuScore(Student stu1, Student stu2); // 比较两个学生的学号
BOOL compareStuNumAsc(Student stu1, Student stu2); // 排序函数
void sortStudents(Student stu[], int count, Function p);

Function.m

 #import "Function.h"

 // 输出数组
void printArray(Student *stu, int count) {
for (int i = ; i < count; i++) {
printf("name = %s, age = %d, score = %.2f, num = %d\n", stu[i].name, stu[i].age, stu[i].score, stu[i].num);
}
} // 比较两个学生的年龄
BOOL compareStuAgeAsc(Student stu1, Student stu2) {
return stu1.age > stu2.age;
} // 比较两个学生的姓名
BOOL compareStuNameAsc(Student stu1, Student stu2) {
return strcmp(stu1.name, stu2.name) > ;
} // 比较两个学生的成绩
BOOL compareStuScore(Student stu1, Student stu2) {
return stu1.score < stu2.score;
} // 比较两个学生的学号
BOOL compareStuNumAsc(Student stu1, Student stu2) {
return stu1.num > stu2.num;
} // 排序函数
void sortStudents(Student stu[], int count, BOOL (*p)(Student, Student)) {
for(int i = ; i < count - ; i++) {
for(int j = ; j < count - - i; j++) {
if(p(stu[j], stu[j + ])) {
Student temp = stu[j];
stu[j] = stu[j + ];
stu[j + ] = temp;
}
}
}
}

main.m

 #import <Foundation/Foundation.h>

 #import "Function.h"

 int main(int argc, const char * argv[]) {
Student stu1 = {"zh", , 9.9, };
Student stu2 = {"s", , , };
Student stu3 = {"f-1", , -, };
Student stu4 = {"Zboomsky", , , };
Student stu5 = {"Boos", , , }; Student stuArray[] = {stu1, stu2, stu3, stu4, stu5};
int count = sizeof(stuArray) / sizeof(Student); // 比较两个学生的年龄
sortStudents(stuArray, count, compareStuAgeAsc);
printArray(stuArray, count); // 比较两个学生的姓名
sortStudents(stuArray, count, compareStuNameAsc);
printArray(stuArray, count); // 比较两个学生的成绩
sortStudents(stuArray, count, compareStuScore);
printArray(stuArray, count); // 比较两个学生的学号
sortStudents(stuArray, count, compareStuNumAsc);
printArray(stuArray, count); return ;
}

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

  1. iOS学习05C语言函数

    本次主要是学习和理解函数,函数树状图如下: 1.函数的声明和定义 函数定义的四要素分别为: 返回值类型 :函数的结果值类型,函数不能返回数组. 指定返回类型是void类型说明函数没有返回值. 函数名 ...

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

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

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

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

  4. C语言函数指针基础

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

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

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

  6. iOS学习之C语言函数指针

    通过函数名调用函数: int max = maxValue(4, 5); printf("max = %d\n", max);     函数类型:int (int, int) 1. ...

  7. C语言基础:函数指针 分类: iOS学习 c语言基础 2015-06-10 21:55 15人阅读 评论(0) 收藏

    函数指针:指向函数的指针变量. 函数名相当于首地址. 函数指针定义:返回值类型  (*函数指针变量名)(参数类型1,参数类型2,....)=初始值 函数指针类型:返回值类型  (*)(参数类型1,参数 ...

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

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

  9. C语言学习笔记之函数指针与函数指针数组

    指针函数:本质是一个函数: 是一个返回指针类型的函数int * sum(){ } 函数指针:本质是一个指针: 是一个指向函数的指针 int (*p)(int,int) = sum; p(5,6); i ...

随机推荐

  1. Innodb之拷贝InnoDB表从一服务器到另一台服务器

    将Innodb类型的表从一台服务器拷贝到另一台服务器,或从一个库拷贝到另一个库. 前提是:innodb_file_per_table =ON. 1 先在目标服务器(库)上创建一个相同的表结构. 如: ...

  2. 关于Linux环境变量

    查看全局变量:  printenv 查看单个环境变量的值可以用echo命令,必须在环境变量的名称前放一个$符号 如:

  3. hdu5878(枚举,打表)

    题目链接:hdu5878 题意:到一行输入t,表示下面有t组数据,然后下面t行每行输入一个数n; 定义x==2^a*3^b*5^c*7^d(a, b, c, d为自然数,x不大于1e+9): 要求对于 ...

  4. hibernate之处理视图

    近期,我去用hibernate去创建视图, 发现无法进立建立视图, 为啥? 个人去尝试去,却发现无法很好的完成, 因为hibernate的作用类似视图 后解决方案是: 1.用传统的方式去处理 2.写存 ...

  5. Python 实现发送、抄送邮件功能

    发送邮件 问题 在web.py中,如何发送邮件? 解法 在web.py中使用web.sendmail()发送邮件. web.sendmail('cookbook@webpy.org', 'user@e ...

  6. 网站性能测试工具--MS Web Application Stress Tool

    MS Web Applicaion Stress Tool 是一款网页测试的性能工具,具体的使用可以参考下面这篇博客文章 http://cuisuqiang.iteye.com/blog/193640 ...

  7. Spring XML配置文件示例(一)——<Servlet name>-servlet.xml

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  8. 一致性hash算法简介与代码实现

    一.简介: 一致性hash算法提出了在动态变化的Cache环境中,判定哈希算法好坏的四个定义: 1.平衡性(Balance) 2.单调性(Monotonicity) 3.分散性(Spread) 4.负 ...

  9. .NET NLog 详解(四) - filter

    我们将版本向前切换到20051025,这期的关注点是filter.我们在使用日志的时候可能希望加上一些过滤器,在满足某些特定条件的时候才输出.举个简单的使用方式如下: <nlog> < ...

  10. bootstrap组件 2

    bootstrap组件2 <!DOCTYPE html> <html lang="zh-cn"> <head > <meta charse ...