当前位置:网站首页>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*) = test
pf先和*结合,是指针,指向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;
}
上述代码使用了回调函数,回调函数就是一个通过函数指针调用的函数。如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用来调用其所指向的函数时,我们就说这是回调函数。回调函数不是由该函数的实现方直接调用,而是在特定的事件或条件发生时由另外的一方调用的,用于对该事件或条件进行响应。
边栏推荐
- hellogolang
- Continuous creation depends on it!
- laravel怎么获取到public路径
- three. JS create cool snow effect
- Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"
- Personal notes of graphics (1)
- js中复选框checkbox如何判定为被选中
- 3000 words speak through HTTP cache
- How to determine whether the checkbox in JS is selected
- AutoLISP series (2): function function 2
猜你喜欢
[Android -- data storage] use SQLite to store data
Mysql database basic operation DQL basic query
记一次项目的迁移过程
Xcode Revoke certificate
AutoLISP series (1): function function 1
Pycharm terminal enables virtual environment
The team of East China Normal University proposed the systematic molecular implementation of convolutional neural network with DNA regulation circuit
HAVE FUN | “飞船计划”活动最新进展
Multiplication in pytorch: mul (), multiply (), matmul (), mm (), MV (), dot ()
spark调优(三):持久化减少二次查询
随机推荐
logback. XML configure logs of different levels and set color output
Performance comparison of tidb for PostgreSQL and yugabytedb on sysbench
How to implement backspace in shell
markdown公式编辑教程
2022 the 4th China (Jinan) International Smart elderly care industry exhibition, Shandong old age Expo
[summary of knowledge] summary of notes on using SVN in PHP
Multiplication in pytorch: mul (), multiply (), matmul (), mm (), MV (), dot ()
laravel 是怎么做到运行 composer dump-autoload 不清空 classmap 映射关系的呢?
asyncio 概念和用法
Leetcode-136-只出现一次的数(用异或来解答)
Shandong old age Expo, 2022 China smart elderly care exhibition, smart elderly care and aging technology exhibition
leetcode 241. Different ways to add parentheses design priority for operational expressions (medium)
logback.xml配置不同级别日志,设置彩色输出
分步式監控平臺zabbix
如何快速检查钢网开口面积比是否符合 IPC7525
Have fun | latest progress of "spacecraft program" activities
You Yuxi, coming!
模拟Servlet的本质
Laravel constructor and middleware execution order
121. The best time to buy and sell stocks