当前位置:网站首页>C语言进阶——函数指针
C语言进阶——函数指针
2022-07-07 14:18:00 【chencli】
我们先看一个代码:
#include<stdio.h>
void test()
{
printf("haha\n");
}
int main()
{
printf("%p\n", test);
printf("%p\n", &test);
return 0;
}
输出的是两个地址,函数名就是函数的地址
将函数的地址存起来:
#include<stdio.h>
void test()
{
printf("haha\n");
}
int main()
{
//函数指针
int (*pf)(const char*) = test;
(*pf)("abc");
pf("abc");
test("abc");
return 0;
}函数指针也是一种指针,是指向函数的指针
int (*pf)(const char*) = testpf先和*结合,是指针,指向test函数,无参数,返回值类型为void
《C陷阱和缺陷》中的一段代码:
( *(void (*)())0 )();void(*)()是函数指针类型
( void (*)() )0 是强制类型转换,结果是函数的地址,0地址中存放一个函数,无参数,无返回值
以上代码总体是一次函数调用,调用的是0作为地址处的函数,首先把0强制转换为无参,返回类型是void的函数的地址,其次是调用0地址处的这个函数
再观察这段代码:
void (*signal(int , void(*)(int)))(int);代码可以化简:
把void(*)(int)重命名为pfun_t
typedef void(*pfun_t)(int);
pfun_t signal(int, pfun_t);signal 与后面的括号结合,是函数名
( int , void(*)(int) ) 是两个参数类型
以上代码是一次函数声明 ,signal函数的第一个参数的类型是int,第二个参数类型是函数指针,该指针指向的是一个参数类型为int,返回值为空的函数,signal函数的返回类型也是一个指针函数,该函数指针也指向的是一个参数类型为int,返回值为空的函数
使用函数指针简化代码:
当功能近似的函数中有较多相同的代码时,可以用函数指针来简化代码
void calc( int(*pf) (int, int) )
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
void menu()
{
printf("*****************\n");
printf("***1.sum 2.sub***\n");
printf("***3.mul 4.div***\n");
printf("*****0.退出*****\n");
printf("*********\n");
}
int add(int x, int y)
{
return x + y;
}
int sub(int x, int y)
{
return x - y;
}
int mul(int x, int y)
{
return x * y;
}
int div(int x, int y)
{
return x / y;
}
//计算
void calc(int(*pf)(int, int))
{
int x = 0;
int y = 0;
int ret = 0;
printf("输入两个操作数:");
scanf("%d%d", &x, &y);
ret = pf(x, y);
printf("%d\n", ret);
}
int main()
{
int input = 0;
do
{
menu();
printf("请选择");
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;
}上述代码使用了回调函数,回调函数就是一个通过函数指针调用的函数。如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用来调用其所指向的函数时,我们就说这是回调函数。回调函数不是由该函数的实现方直接调用,而是在特定的事件或条件发生时由另外的一方调用的,用于对该事件或条件进行响应。
边栏推荐
- Laravel5.1 Routing - routing packets
- Aerospace Hongtu information won the bid for the database system research and development project of a unit in Urumqi
- 【知识小结】PHP使用svn笔记总结
- 应用程序和matlab的通信方式
- SysOM 案例解析:消失的内存都去哪了 !| 龙蜥技术
- AutoLISP series (1): function function 1
- Lecturer solicitation order | Apache seatunnel (cultivating) meetup sharing guests are in hot Recruitment!
- MySQL数据库基本操作-DQL-基本查询
- Balanced binary tree (AVL)
- TCP framework___ Unity
猜你喜欢

平衡二叉树(AVL)

Pycharm terminal enables virtual environment

Statistical learning method -- perceptron

Logback日志框架第三方jar包 免费获取

Imitate the choice of enterprise wechat conference room

Performance comparison of tidb for PostgreSQL and yugabytedb on sysbench

Three. JS series (1): API structure diagram-1

2022 the 4th China (Jinan) International Smart elderly care industry exhibition, Shandong old age Expo

Personal notes of graphics (1)

Vs tool word highlight with margin
随机推荐
You Yuxi, coming!
PHP realizes wechat applet face recognition and face brushing login function
Xcode Revoke certificate
logback. XML configure logs of different levels and set color output
AutoLISP series (1): function function 1
谈谈 SAP iRPA Studio 创建的本地项目的云端部署问题
Common training data set formats for target tracking
Talk about the cloud deployment of local projects created by SAP IRPA studio
Three. JS series (1): API structure diagram-1
Spark Tuning (III): persistence reduces secondary queries
Is it reliable to open an account on Tongda letter with your mobile phone? Is there any potential safety hazard in such stock speculation
统计学习方法——感知机
47_Opencv中的轮廓查找 cv::findContours()
Three. JS series (3): porting shaders in shadertoy
Leetcode-231-2的幂
Power of leetcode-231-2
Apache Doris just "graduated": why should we pay attention to this kind of SQL data warehouse?
laravel post提交数据时显示异常
Opportunity interview experience summary
What about the pointer in neural network C language