当前位置:网站首页>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;
}上述代码使用了回调函数,回调函数就是一个通过函数指针调用的函数。如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用来调用其所指向的函数时,我们就说这是回调函数。回调函数不是由该函数的实现方直接调用,而是在特定的事件或条件发生时由另外的一方调用的,用于对该事件或条件进行响应。
边栏推荐
- AutoLISP series (3): function function 3
- 95. (cesium chapter) cesium dynamic monomer-3d building (building)
- [C language] question set of X
- "The" "PIP" "entry cannot be recognized as the name of a cmdlet, function, script file, or runnable program."
- IP地址和物理地址有什么区别
- 统计学习方法——感知机
- Sysom case analysis: where is the missing memory| Dragon lizard Technology
- iptables只允许指定ip地址访问指定端口
- Regular expression string
- Imitate the choice of enterprise wechat conference room
猜你喜欢

Lecturer solicitation order | Apache seatunnel (cultivating) meetup sharing guests are in hot Recruitment!

【C 语言】 题集 of Ⅹ

Enterprise log analysis system elk
![[Android -- data storage] use SQLite to store data](/img/f6/a4930276b3da25aad3ab1ae6f1cf49.png)
[Android -- data storage] use SQLite to store data

Balanced binary tree (AVL)

PyTorch 中的乘法:mul()、multiply()、matmul()、mm()、mv()、dot()

Odoo集成Plausible埋码监控平台

Performance comparison of tidb for PostgreSQL and yugabytedb on sysbench

谈谈 SAP iRPA Studio 创建的本地项目的云端部署问题

模仿企业微信会议室选择
随机推荐
SPI master RX time out interrupt
面试题 01.02. 判定是否互为字符重排-辅助数组算法
95. (cesium chapter) cesium dynamic monomer-3d building (building)
Personal notes of graphics (3)
全网“追杀”钟薛高
一个普通人除了去工厂上班赚钱,还能干什么工作?
AutoLISP series (1): function function 1
Personal notes of graphics (4)
Have fun | latest progress of "spacecraft program" activities
Tidb cannot start after modifying the configuration file
2022 the 4th China (Jinan) International Smart elderly care industry exhibition, Shandong old age Expo
The team of East China Normal University proposed the systematic molecular implementation of convolutional neural network with DNA regulation circuit
laravel中将session由文件保存改为数据库保存
Strengthen real-time data management, and the British software helps the security construction of the medical insurance platform
Find tags in prefab in unity editing mode
47_Opencv中的轮廓查找 cv::findContours()
Usage of config in laravel
Odoo集成Plausible埋码监控平台
Markdown formula editing tutorial
hellogolang