当前位置:网站首页>函数指针和指针函数
函数指针和指针函数
2022-04-22 07:43:00 【weixin_46272577】
指针函数
返回指针的函数
int* array_() {
int* ptr = (int*)malloc(10 * sizeof(int));
for (int i = 0; i < 10; i++) {
*(ptr + i) = 10;
}
return ptr;
}
int main() {
int* temp = array_();
for (int i = 0; i < 10; i++) {
cout << *(temp + i) << " ";
}
cout << endl;
return 0;
}
10 10 10 10 10 10 10 10 10 10
函数指针
声明函数指针
// 声明一个函数指针
void (*foo) ();

如图所示,函数指针的声明
注意,不要混淆函数指针和指针函数
int (*fptrl)(int, int); //指向函数的指针
int* function(int, int); //返回指针类型的函数
我们还可以使用 typedef声明一个类型定义
typedef int (*funcptr)(int,int);//funcptr 为这个类型
funcptr fptr2 = sum;
通过函数指针来调用函数
函数指针存储函数的地址,用于指向函数,可以借由函数指针来调用不同的函数
但是函数指针和其所指向的函数的类型必须一致,也就是说,其返回值类型和参数类型和参数个数需一致
//声明函数指针
typedef int (*function_ptr)(int, int);
//需要被指向的函数
int sum(int a, int b) {
return a + b;
}
int main()
{
//fptrl = sum2;类型不匹配,这个返回的是double类型,接受的也是两个double的参数
function_ptr fptrl = sum;
cout << fptrl(2, 3) << endl; // 5
return 0;
}
在这个案例中,我们使用了函数指针来调用函数,但是我们没有在函数前面加&,函数名和数组名类似,可以直接返回该函数的地址
int (*fptrl) (int);
int square(int num) {
return num * num;
}
int main()
{
fptrl = square;
cout << fptrl(2) << endl; //4
return 0;
}

这是调用另一个函数的例子,理解一下内存中函数是怎么被调用的
传递函数指针
只要把函数指针声明作为函数参数即可
//声明函数指针
typedef int (*fptrOperation)(int, int);
//加法
int add(int num1, int num2) {
return num1 + num2;
}
//减法
int sub(int num1, int num2) {
return num1 - num2;
}
//接受函数指针,还有用作计算的两个int参数
int compute(fptrOperation operation, int a, int b) {
return operation(a, b);//返回操作的结果
}
int main()
{
cout << compute(add, 5, 6);
cout << compute(sub, 5, 6);
/*fptrOperation fptr = sum; cout << "the result(+) is: " << compute(add, 10, 5) << endl; cout << "the result(+) is: " << compute(fptr, 10, 5) << endl; cout << "the result(+) is: " << evaluate('+', 10, 5) << endl; fptr = sub; cout << "the result(-) is: " << compute(sub, 10, 5) << endl; cout << "the result(-) is: " << compute(fptr, 10, 5) << endl; cout << "the result(+) is: " << evaluate('-', 10, 5) << endl;*/
return 0;
}
返回函数指针
typedef int (*fptrOperation)(int, int);
//加法
int add(int num1, int num2) {
return num1 + num2;
}
//减法
int sub(int num1, int num2) {
return num1 - num2;
}
//计算
int compute(fptrOperation operation, int a, int b) {
return operation(a, b);
}
//根据字符返回函数指针
fptrOperation select(char opcode) {
switch (opcode) {
case '+': return add;
case '-': return sub;
}
}
//接受字符并调用select函数,接受select返回的函数指针,调用该函数指针指向的函数
int evaluate(char opcode, int num1, int num2) {
fptrOperation operation = select(opcode);
return operation(num1, num2);
}
int main()
{
fptrOperation fptr = add;
cout << "the result(+) is: " << compute(add, 10, 5) << endl;
cout << "the result(+) is: " << compute(fptr, 10, 5) << endl;
cout << "the result(+) is: " << evaluate('+', 10, 5) << endl;
fptr = sub;
cout << "the result(-) is: " << compute(sub, 10, 5) << endl;
cout << "the result(-) is: " << compute(fptr, 10, 5) << endl;
cout << "the result(+) is: " << evaluate('-', 10, 5) << endl;
return 0;
}
看到这里,学过C++的是否觉得有点眼熟,这不就是多态那味嘛~
所以说,打好基础很重要!
版权声明
本文为[weixin_46272577]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_46272577/article/details/116485653
边栏推荐
- vscode的插件
- Pycharm terminal PIP installation error: "PIP" item is recognized as the name of cmdlet, function, script file or runnable program
- Redefine China's "core"
- spark sql 获取数组某index处元素
- I feel that writing a paper is a waste of time. Do I want to write a paper?
- kubernetes—实战入门
- One question per day: improve the mold assembly on the 15th day of sprint in the big factory
- 236. 二叉树的最近公共祖先(Medium)
- Fresco简单的使用—SimpleDraweeView
- 静态库修改初探
猜你喜欢

129. 求根节点到叶节点数字之和(Medium)

Simple use of fresco - simpledraweeview

概率论笔记6.3抽样分布

7-34 删除重复字符(set用法)&&7-35 统计字符出现次数(unordered_map)

Teach you how to realize the pull-down refresh and pull-up load of recyclerview

Flutter Modul类与Json相互转换

235. 二叉搜索树的最近公共祖先(Easy)

PWM output of STM32 to steering gear sg90

Make the airtest ide into a script and make the script run

Nacos Foundation (4): configure the external database of Nacos
随机推荐
MaterialApp
只有服务器,没有域名,怎么部署网站?
LeetCode_ 118. Yanghui triangle_ Dynamic programming_ Int * * learning
centos7安装MySQL8.0
SmartTabLayout 简介
Under the new retail development trend, how to operate and promote the social e-commerce platform?
cesium中实现楼层分解动画
shell脚本中ps -ef查询进程PID一直返回异常
CAS: 36530-06-0 boron chloride phthalocyanine | phthalocyanine | boron chloride phthalocyanine | dichloroboron phthalocyanine dye | boron chloride phthalocyanine | boronsubphalocyaninichloride
Typescript学习指南
Viewpager comprehensive summary
58 Technology Salon issue 28 - anjuke quality assurance system Salon
技术选型分类(2022)
【论文阅读】【3d目标检测】Voxel Set Transformer: A Set-to-Set Approach to 3D Object Detection from Point Clouds
[跟着官方文档学JUnit5][二][WritingTests][学习笔记]
liunx基础—zabbix5.0监控系统安装部署
qt designer 跳转,布局,样式
Golang学习,指针,循环控制相关
Flutter judges network availability
重新定义中国“芯”