当前位置:网站首页>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;
}上述代码使用了回调函数,回调函数就是一个通过函数指针调用的函数。如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用来调用其所指向的函数时,我们就说这是回调函数。回调函数不是由该函数的实现方直接调用,而是在特定的事件或条件发生时由另外的一方调用的,用于对该事件或条件进行响应。
边栏推荐
- Personal notes of graphics (1)
- Power of leetcode-231-2
- 95. (cesium chapter) cesium dynamic monomer-3d building (building)
- 95.(cesium篇)cesium动态单体化-3D建筑物(楼栋)
- How to implement backspace in shell
- PHP实现微信小程序人脸识别刷脸登录功能
- 华东师大团队提出,具有DNA调控电路的卷积神经网络的系统分子实现
- 121. 买卖股票的最佳时机
- [C language] question set of X
- AutoLISP series (2): function function 2
猜你喜欢

Prediction - Grey Prediction

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

分步式监控平台zabbix

平衡二叉树(AVL)

The team of East China Normal University proposed the systematic molecular implementation of convolutional neural network with DNA regulation circuit

Vs tool word highlight with margin

SPI master RX time out interrupt

Personal notes of graphics (2)

AutoLISP series (3): function function 3

记一次项目的迁移过程
随机推荐
The team of East China Normal University proposed the systematic molecular implementation of convolutional neural network with DNA regulation circuit
logback.xml配置不同级别日志,设置彩色输出
HAVE FUN | “飞船计划”活动最新进展
ThinkPHP URL 路由简介
JS中null NaN undefined这三个值有什么区别
3000 words speak through HTTP cache
Leetcode-136-只出现一次的数(用异或来解答)
Balanced binary tree (AVL)
Plate - forme de surveillance par étapes zabbix
PHP realizes wechat applet face recognition and face brushing login function
Continuous creation depends on it!
Laravel 中config的用法
How can laravel get the public path
Notification uses full resolution
Odoo integrated plausible embedded code monitoring platform
SysOM 案例解析:消失的内存都去哪了 !| 龙蜥技术
Rongyun won the 2022 China Xinchuang digital office portal excellence product award!
记一次项目的迁移过程
Sysom case analysis: where is the missing memory| Dragon lizard Technology
What is the difference between IP address and physical address