//函数指针的定义:指向函数的指针成为函数指针,用来存储函数的地址(即首地址,也就是函数名,因为首地址就是函数的入口地址)
//函数调用 第一种方法:直接调用函数名,传入相应的参数即可
int x = 20,y = 8;
int max = compareAB(x, y);
printf("max = %d\n",max);
//第二种方法:使用函数指针来调用,先定义一个指向compareAB函数的指针
//定义函数指针的一般形式:
/*
返回值类型 (*变量名)(参数列表) = 函数名(函数的首地址);
'*' 不可省略
int (*p)(int a, int b) = NULL;
p = 函数名;
*/
int a = 20,b = 40;
int (*p)(int a, int b) = NULL;
p = compareAB;//指针变量p指向compareAB函数
int maxab = p(a,b);//通过指针变量调用compareAB函数
printf("maxab = %d\n",maxab);
//1.无参无返回值
int tomoney = 200;
getmoney(tomoney);
//2.无返回值有参数 通过函数指针调用
int money = 90;
void (*p2)(int x) = NULL;
p2 = getmoney;//p2指向getmoney函数的首地址
p2(money);
//3.无参有返回值 通过函数指针调用
char *(*p)() = NULL;
p = getPString;//把函数的首地址赋给p
char *pReceive = p();
printf("%s\n");
//定义两个函数,一个求最大值一个求和一个求最小值,输入max或sum分别求6和10的最大值和和和最小值
int x = 6,y = 10;
char temp[] = {0};设置输入时的字符串
int (*p)(int a , int b ) = NULL;
printf("请输入字符串 sum(求和) min(最小值) max(最大值):");
scanf("%s",temp);输入字符串
如果此时输入的字符串等于sum,那么就将求和函数的函数名赋值给p(首地址)
if (strcmp(temp, "sum") == 0) {
p = sumValue;
}else if (strcmp(temp, "min") == 0){
p = minvalue;求最小值 存储的是minValue的首地址
}else if (strcmp(temp, "max") == 0){
p = maxvalue;求最大值 存储的是maxValue的首地址
}else{
printf("输入有误");
return 0;
}
int result = p(x,y);使用函数指针调用函数
printf("result = %d",result);
//回调函数
//如果把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用来调用它所指向的函数时,我们就说这是回调函数
//函数指针作为另一个函数的参数,也就是函数的首地址作为另一个函数的参数
int b
= 2,c = 3;
int (*p5)(int a , int b, int(*p)(int ,int)) = NULL;
p5 = getValue;//把getValue的首地址赋给p5
int valueResult = p5(x,y,maxvalue);
int (*p5)(int a , int b, int(*p)(int ,int)) = NULL;
p5 = getValue;//把getValue的首地址赋给p5
int valueResult = p5(x,y,maxvalue);
printf("%d\n",valueResult);
//三:动态排序
//练习:创建一个有5个学生的学生体结构,其中包括姓名、年龄、体重、身高、分数,分别按照这五个属性排序
//创建一个学生结构体
typedef struct student{
char name[20];//姓名
int age;//年龄
float weight;//体重
float height;//身高
float score;//分数
}Stu;
//为什么使用动态排序,动态排序的好处:所有的排序函数if语句之后的比较条件不一样,其余的所有代码都是相同的,把相同的内容放在一个函数里,只写一遍即可;把if语句后面不同的条件写个一个个的条件函数 以供回调即可
//根据年龄判断
BOOL sortStudentByAge(Stu stu1,Stu stu2);
BOOL sortStudentByAge(Stu stu1,Stu stu2){
return stu1.age > stu2.age;
}
//根据体重判断
BOOL sortStudentByWeight(Stu stu1,Stu stu2);
BOOL sortStudentByWeight(Stu stu1,Stu stu2){
return stu1.weight > stu2.weight;
}
//根据身高判断
BOOL sortStudentByHeight(Stu stu1,Stu stu2);
BOOL sortStudentByHeight(Stu stu1,Stu stu2){
return stu1.height > stu2.height;
}
//根据分数判断
BOOL sortStudentByScore(Stu stu1,Stu stu2);
BOOL sortStudentByScore(Stu stu1,Stu stu2){
return stu1.score < stu2.score;
}
typedef BOOL (*SORT)(Stu,Stu);//重定义
void sortStudent(Stu *p,int count,SORT p2){
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - 1 - i; j++) {
//函数指针调用相应的函数
if (p2(*(p + j),*(p + j + 1))) {
Stu temp = *(p + j);
*(p + j) = *(p + j + 1);
*(p + j + 1) = temp;
}
}
}
}
//输出排序后的学生顺序
void printAllSortStudent(Stu *p,int count){
for (int i = 0; i < count; i++) {
printf("姓名:%s 年龄:%d 体重:%.2f 身高:%.2f 分数:%.2f\n",(p + i) -> name,(p + i) -> age,(p + i) -> weight,(p + i) -> height,(p + i) -> score);
}
}
int main(int argc, const char * argv[]) {
Stu stus[5] = {
{"HaiFeng",21,130,175,89},
{"ShiYang",12,190,155,18},
{"YaNan",38,90,200,60},
{"JiangTao",49,249,188,99},
{"XiaoLong",5,30,160,100}
};
sortStudent(stus, 5, sortStudentByHeight);
printf("\n按照身高排序好的学生顺序为:\n");
printAllSortStudent(stus, 5);
sortStudent(stus, 5, sortStudentByWeight);
printf("\n按照体重排序好的学生顺序为:\n");
printAllSortStudent(stus, 5);
sortStudent(stus, 5, sortStudentByAge);
printf("\n按照年龄排序好的学生顺序为:\n");
printAllSortStudent(stus, 5);
sortStudent(stus, 5, sortStudentByScore);
printf("\n按照分数排序好的学生顺序为:\n");
printAllSortStudent(stus, 5);
typedef struct student{
char name[20];//姓名
int age;//年龄
float weight;//体重
float height;//身高
float score;//分数
}Stu;
//为什么使用动态排序,动态排序的好处:所有的排序函数if语句之后的比较条件不一样,其余的所有代码都是相同的,把相同的内容放在一个函数里,只写一遍即可;把if语句后面不同的条件写个一个个的条件函数 以供回调即可
//根据年龄判断
BOOL sortStudentByAge(Stu stu1,Stu stu2);
BOOL sortStudentByAge(Stu stu1,Stu stu2){
return stu1.age > stu2.age;
}
//根据体重判断
BOOL sortStudentByWeight(Stu stu1,Stu stu2);
BOOL sortStudentByWeight(Stu stu1,Stu stu2){
return stu1.weight > stu2.weight;
}
//根据身高判断
BOOL sortStudentByHeight(Stu stu1,Stu stu2);
BOOL sortStudentByHeight(Stu stu1,Stu stu2){
return stu1.height > stu2.height;
}
//根据分数判断
BOOL sortStudentByScore(Stu stu1,Stu stu2);
BOOL sortStudentByScore(Stu stu1,Stu stu2){
return stu1.score < stu2.score;
}
typedef BOOL (*SORT)(Stu,Stu);//重定义
void sortStudent(Stu *p,int count,SORT p2){
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - 1 - i; j++) {
//函数指针调用相应的函数
if (p2(*(p + j),*(p + j + 1))) {
Stu temp = *(p + j);
*(p + j) = *(p + j + 1);
*(p + j + 1) = temp;
}
}
}
}
//输出排序后的学生顺序
void printAllSortStudent(Stu *p,int count){
for (int i = 0; i < count; i++) {
printf("姓名:%s 年龄:%d 体重:%.2f 身高:%.2f 分数:%.2f\n",(p + i) -> name,(p + i) -> age,(p + i) -> weight,(p + i) -> height,(p + i) -> score);
}
}
int main(int argc, const char * argv[]) {
Stu stus[5] = {
{"HaiFeng",21,130,175,89},
{"ShiYang",12,190,155,18},
{"YaNan",38,90,200,60},
{"JiangTao",49,249,188,99},
{"XiaoLong",5,30,160,100}
};
sortStudent(stus, 5, sortStudentByHeight);
printf("\n按照身高排序好的学生顺序为:\n");
printAllSortStudent(stus, 5);
sortStudent(stus, 5, sortStudentByWeight);
printf("\n按照体重排序好的学生顺序为:\n");
printAllSortStudent(stus, 5);
sortStudent(stus, 5, sortStudentByAge);
printf("\n按照年龄排序好的学生顺序为:\n");
printAllSortStudent(stus, 5);
sortStudent(stus, 5, sortStudentByScore);
printf("\n按照分数排序好的学生顺序为:\n");
printAllSortStudent(stus, 5);
return 0;
}
本文介绍了iOS开发中如何使用函数指针进行函数调用,包括直接调用和通过指针调用,以及如何实现动态排序。此外,还探讨了函数回调的概念,并给出了动态排序学生结构体的示例,展示了根据不同属性(年龄、体重、身高、分数)进行排序的方法。
1636

被折叠的 条评论
为什么被折叠?



