当前位置: 首页 > news >正文

深入了解指针(6)

文章目录

    • 1.函数指针数组
    • 2.转移表
    • 3.回调函数

1.函数指针数组

存放函数指针的数组

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int add(int x, int y)
{return x + y;
}
int sub(int x, int y)
{return x - y;
}int main()
{int (*p1)(int x, int y) = add;int (*p2)(int x, int y) = sub;//在这里p1与p2的类型相同我们可以把其放在数组int (*p[4])(int, int) = { add,sub };//这里[4]代表函数指针数组大小可以存放4个函数指针。printf("%d\n",p[0](20, 30));printf("%d\n", p[1](20, 30));return 0;
}

在这里插入图片描述

2.转移表

函数指针数组的应用

首先使用数组的方式实现一个简易的计算功能

 void meau()
{printf("         开始选择        \n");printf("**** 1.add    2.sub  ****\n");printf("**** 3.mul    4.div  ****\n");printf("****    0.exit       ****\n");printf("                         \n");}
int add(int a, int b)//加法
{return a + b;
}
int sub(int a, int b)//减法
{return a - b;
}
int mul(int a, int b)//乘法
{return a * b;
}
int div(int a, int b)//除法
{return a /b;
}
void calc(int(*p)(int a, int b))
{int a = 0; int b = 0; int c = 0;printf("请输入两个整数进行运算\n");scanf("%d %d", &a, &b);c = p(a, b);printf("运算结果=%d\n", c);
}
int main()
{int input = 1;
do{int a = 0; int b = 0; int c = 0;meau();scanf("%d", &input);switch(input){case 1:printf("请输入两个整数进行运算\n");scanf("%d %d", &a, &b);c = add(a, b);printf("运算结果=%d\n", c);break;case 2:printf("请输入两个整数进行运算\n");scanf("%d %d", &a, &b);c = sub(a, b);printf("运算结果=%d\n", c);break;case 3:printf("请输入两个整数进行运算\n");scanf("%d %d", &a, &b);c = mul(a, b);printf("运算结果=%d\n", c);break;case 4:printf("请输入两个整数进行运算\n");scanf("%d %d", &a, &b);c = div(a, b);printf("运算结果=%d\n", c);break;case 0:printf("退出计算\n"); break;default :printf("选择错误请重新选择/n");	break;}
} while (input);return 0;
}

用函数指针数组的方式实现一个转移的效果

void meau()
{printf("         开始选择        \n");printf("**** 1.add    2.sub  ****\n");printf("**** 3.mul    4.div  ****\n");printf("****    0.exit       ****\n");printf("                         \n");}
int add(int a, int b)//加法
{return a + b;
}
int sub(int a, int b)//减法
{return a - b;
}
int mul(int a, int b)//乘法
{return a * b;
}
int div(int a, int b)//除法
{return a / b;
}
void calc(int(*p)(int a, int b))
{int a = 0; int b = 0; int c = 0;printf("请输入两个整数进行运算\n");scanf("%d %d", &a, &b);c = p(a, b);printf("运算结果=%d\n", c);
}
int main()
{int input = 1;do{meau();scanf("%d", &input);switch (input){case 1:calc(add);break;case 2:calc(sub);break;case 3:calc(mul);break;case 4:calc(div);break;case 0:printf("退出计算\n");break;default:printf("选择错误请重新选择/n");break;}} while (input);return 0;
}

在这里插入图片描述

3.回调函数

回调函数就是⼀个通过函数指针调⽤的函数。
如果你把函数的指针(地址)作为参数传递给另⼀个函数,当这个指针被⽤来调⽤其所指向的函数时,被调⽤的函数就是回调函数。回调函数不是由该函数的实现⽅直接调⽤,⽽是在特定的事件或条件发⽣时由另外的⼀⽅调⽤的,⽤于对该事件或条件进⾏响应。

在上面转移表的实现中已经使用的回调函数接下来给诠释一下
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里列如 int add(int x,int y)——到void calc(int(*p)(int a, int b))——c=p(a,b);
这就是一次回调函数的实现。

持续指针系列

http://www.lryc.cn/news/424010.html

相关文章:

  • 【Linux系列】known_hosts详解
  • MySQL4 多表查询 内连接
  • 力扣45:跳跃游戏2题解
  • 产品经理-产品被同行抄袭如何处理(32)
  • LDR6020在Type-C手机同时充电与USB2.0数据传输方案
  • Python学习笔记(七)
  • mysql中log
  • iOS Object-C 创建类别(Category) 与使用
  • Rocky系统部署k8s1.28.2单节点集群(Containerd)+Kuboard
  • Springboot整合Flowable入门-学习笔记
  • C语言常见的题目
  • Android13适配记录
  • Android TV上OTT PWA应用开发的播放器选择:video.js vs exoplayer
  • 24.8.14 《CLR via C#》 笔记12
  • P2801 教主的魔法
  • Go 语言channel的应用场景及使用技巧
  • QLabel设置图像的方法+绘制文本换行显示
  • LVS原理及相关配置
  • webrtc一对一视频通话功能实现
  • 通道(channel)传递数据的例子写一个
  • Vue3+Echarts+饼图环形图
  • Python while编程题目|AI悦创Python一对一教学辅导
  • C语言 | Leetcode C语言题解之第324题摆动排序II
  • Docker③_VMware虚拟机和Docker的备份与恢复
  • 【EMC专题】ESD抑制器简要介绍
  • 贷齐乐系统最新版SQL注入(绕过WAF可union select跨表查询)
  • 『大模型笔记』虚拟机(Virtual Machine,VM)与Docker对比!
  • 基于SpringBoot+Vue框架的租车管理系统
  • HAProxy基本配置及参数实操
  • go-zero中间件的使用