当前位置:网站首页>Advanced C language -- function pointer
Advanced C language -- function pointer
2022-07-07 16:35:00 【chencli】
Let's look at a code first :
#include<stdio.h>
void test()
{
printf("haha\n");
}
int main()
{
printf("%p\n", test);
printf("%p\n", &test);
return 0;
}
The output is two addresses , The name of the function is the address of the function
Save the address of the function :
#include<stdio.h>
void test()
{
printf("haha\n");
}
int main()
{
// A function pointer
int (*pf)(const char*) = test;
(*pf)("abc");
pf("abc");
test("abc");
return 0;
}Function pointers are also pointers , Is a pointer to a function
int (*pf)(const char*) = testpf The first and * combination , Is a pointer , Point to test function , No parameter , The return value type is void
《C Pitfalls and pitfalls 》 A piece of code :
( *(void (*)())0 )();void(*)() It's a function pointer type
( void (*)() )0 Is a cast , The result is the address of the function ,0 Store a function in the address , No parameter , No return value
The above code is generally a function call , It's called 0 As a function at the address , First turn on the 0 Cast to no parameter , The return type is void The address of the function , The second is to call 0 This function at the address
Look at this code again :
void (*signal(int , void(*)(int)))(int);Code can be simplified :
hold void(*)(int) Rename it to pfun_t
typedef void(*pfun_t)(int);
pfun_t signal(int, pfun_t);signal Combine with the following parentheses , Is the function name
( int , void(*)(int) ) Are two parameter types
The above code is a function declaration ,signal The type of the first argument to the function is int, The second parameter type is the function pointer , The pointer points to a parameter of type int, Function with null return value ,signal The return type of a function is also a pointer function , The function pointer also points to a parameter of type int, Function with null return value
Use function pointers to simplify code :
When there are more identical codes in functions with similar functions , Function pointers can be used to simplify code
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. sign out *****\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;
}
// Calculation
void calc(int(*pf)(int, int))
{
int x = 0;
int y = 0;
int ret = 0;
printf(" Enter two operands :");
scanf("%d%d", &x, &y);
ret = pf(x, y);
printf("%d\n", ret);
}
int main()
{
int input = 0;
do
{
menu();
printf(" Please select ");
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(" Exit calculator !\n");
break;
default:
printf(" Wrong choice !\n");
break;
}
} while (input);
return 0;
}The above code uses the callback function , Callback function It's a pass through A function pointer Called function . If you put Pointer to function ( Address ) Pass as argument to another function , When this pointer is used to call the function it points to , Let's just say this is a callback function . The callback function is not called directly by the function's implementer , It's called by another party when a particular event or condition occurs , Used to respond to the event or condition .
边栏推荐
- 【HCSD大咖直播】亲授大厂面试秘诀-简要笔记
- Logback logging framework third-party jar package is available for free
- three. JS create cool snow effect
- JS 模块化
- IP地址和物理地址有什么区别
- 3000 words speak through HTTP cache
- pycharm 终端部启用虚拟环境
- prometheus api删除某个指定job的所有数据
- How to determine whether the checkbox in JS is selected
- Cesium (4): the reason why gltf model is very dark after loading
猜你喜欢

二叉搜索树(特性篇)

"The" "PIP" "entry cannot be recognized as the name of a cmdlet, function, script file, or runnable program."

Leetcode-231-2的幂

Odoo集成Plausible埋码监控平台
Notification uses full resolution

Vs2019 configuration matrix library eigen

Prediction - Grey Prediction

删除 console 语句引发的惨案

预测——灰色预测

记录Servlet学习时的一次乱码
随机推荐
Performance comparison of tidb for PostgreSQL and yugabytedb on sysbench
Laravel 服务提供者实例教程 —— 创建 Service Provider 测试实例
Iptables only allows the specified IP address to access the specified port
URL和URI的关系
Enterprise log analysis system elk
[summary of knowledge] summary of notes on using SVN in PHP
华东师大团队提出,具有DNA调控电路的卷积神经网络的系统分子实现
应用程序和matlab的通信方式
three.js打造酷炫下雪效果
Logback logging framework third-party jar package is available for free
Three. JS series (2): API structure diagram-2
laravel怎么获取到public路径
torch.numel作用
Good news! Kelan sundb database and Hongshu technology privacy data protection management software complete compatibility adaptation
The differences between exit, exit (0), exit (1), exit ('0 '), exit ('1'), die and return in PHP
【DesignMode】模板方法模式(Template method pattern)
Spark Tuning (III): persistence reduces secondary queries
Tidb cannot start after modifying the configuration file
模拟Servlet的本质
打造All-in-One应用开发平台,轻流树立无代码行业标杆