当前位置:网站首页>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;
}上述代码使用了回调函数,回调函数就是一个通过函数指针调用的函数。如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用来调用其所指向的函数时,我们就说这是回调函数。回调函数不是由该函数的实现方直接调用,而是在特定的事件或条件发生时由另外的一方调用的,用于对该事件或条件进行响应。
边栏推荐
- How to implement backspace in shell
- Common training data set formats for target tracking
- TCP framework___ Unity
- Set the route and optimize the URL in thinkphp3.2.3
- 企业级日志分析系统ELK
- The team of East China Normal University proposed the systematic molecular implementation of convolutional neural network with DNA regulation circuit
- ThinkPHP URL 路由简介
- Record the migration process of a project
- 预测——灰色预测
- Sysom case analysis: where is the missing memory| Dragon lizard Technology
猜你喜欢

SPI master RX time out interrupt
Notification uses full resolution

Eye of depth (VII) -- Elementary Transformation of matrix (attachment: explanation of some mathematical models)

Tragedy caused by deleting the console statement

分步式监控平台zabbix

统计学习方法——感知机

无法将“pip”项识别为 cmdlet、函数、脚本文件或可运行程序的名称

AutoLISP series (2): function function 2
![[vulnhub range] thales:1](/img/fb/721d08697afe9b26c94fede628c4d1.png)
[vulnhub range] thales:1

torch.numel作用
随机推荐
Description of vs common shortcut keys
Shandong old age Expo, 2022 China smart elderly care exhibition, smart elderly care and aging technology exhibition
Bidding announcement: 2022 Yunnan Unicom gbase database maintenance public comparison and selection project (second) comparison and selection announcement
[C language] question set of X
laravel构造函数和中间件执行顺序问题
PHP中exit,exit(0),exit(1),exit(‘0’),exit(‘1’),die,return的区别
three.js打造酷炫下雪效果
laravel post提交数据时显示异常
MySQL中, 如何查询某一天, 某一月, 某一年的数据
【HCSD大咖直播】亲授大厂面试秘诀-简要笔记
Tidb cannot start after modifying the configuration file
php 自带过滤和转义函数
2022 the 4th China (Jinan) International Smart elderly care industry exhibition, Shandong old age Expo
Laravel 服务提供者实例教程 —— 创建 Service Provider 测试实例
Three. JS series (3): porting shaders in shadertoy
面试题 01.02. 判定是否互为字符重排-辅助数组算法
[Android -- data storage] use SQLite to store data
TCP framework___ Unity
删除 console 语句引发的惨案
Performance comparison of tidb for PostgreSQL and yugabytedb on sysbench