当前位置:网站首页>Function pointer and callback function
Function pointer and callback function
2022-07-29 04:02:00 【Xiaowa 123】
1. Callback function : Functions called through function pointers , A function as a function parameter ,C Callback function in language can only be realized through function pointer .
Watch out! : The callback function is called through the function pointer .
2. A function pointer —— Benefits of callback functions :
(1) The caller who calls the callback function does not need to know how the callback function realizes its function , You only need to know that this function can realize some function , This should be API Interface .
(2) as everyone knows , The function name is also a pointer , Point to the entry address of the function , It's a pointer constant , This is similar to the array name . Since the function name itself is a pointer , So why define a function pointer ? Aren't they both pointers ?
3. A function pointer - The use of callback functions
#include<stdio.h>
void CallBacksFunc1()
{
printf("this is callbacksfunc1\n");
}
void CallBacksFunc2()
{
printf("this is callbacksfunc2\n");
}
void CallBacksFunc3()
{
printf("this is callbacksfunc3\n");
}
void Handle(void(*p)())// function Handle() Call the callback function through the function pointer .
from Handle You can see from the parameter list of , The parameter of the function that calls the callback function is a function pointer , No specific function name is required , That is to say, there is no requirement to call any function , Only function types and parameter types are limited , As long as it is a function that conforms to the corresponding function type and parameter type , Its function name can be used as the parameter here .
therefore , This is very convenient to achieve one thing , And it is very convenient to call different callback functions , Just use the function names of different callback functions .
{
printf("Entering Handle\n");
p();// Call the callback function through the function pointer
printf("Leaving Hande\n");
}
int main(int agrc, char* argv[])
{
Handle(CallBacksFunc1);// Note that only the function name of the callback function is written here
The benefits of callback functions are reflected here , Switch to CallBackFunc2 It's called CallBackFunc2, But not right. Handle Modify the parameter list of the function . If Handle There are specific function names in the parameter list of , That would require several modifications .
return 0;
}in general , If you use the method of function pointer to call the callback function , You can save space , Between the designer of the calling function and the designer of the called function, the function pointer is used as API Interface , Design separately .
May refer to :https://blog.csdn.net/weixin_41042404/article/details/86768240
https://blog.csdn.net/weixin_39939425/article/details/90298435
https://blog.csdn.net/wujiangguizhen/article/details/17153495
This is good https://www.jianshu.com/p/8c63d939285b
边栏推荐
- SQL server当存储过程接收的参数是int类型时,如何做判断?
- Typescript from getting started to mastering (XX) function generics
- [redis series] string data structure
- LVS+KeepAlived高可用部署实战应用
- 华为天才少年稚晖君做了一把模块化机械键盘,引起极客圈地震,网友:这才是真正的客制化...
- 内连接和左连接简单案例
- Form verification of landline
- First knowledge of C language (3)
- 安装ros的laser_scan_matche库所遇到的问题(一)
- Object array merges elements according to a field
猜你喜欢

Basic configuration of BGP - establish peers and route announcements

SSL==证书相关概念

新零售O2O 电商模式解析

Analysis of new retail o2o e-commerce model

BGP的基础配置---建立对等体、路由宣告

Why BGP server is used in sunflower remote control? Automatic optimal route and high-speed transmission across operators

Nacos registry

1985-2020 (8 Editions) global surface coverage download and introduction

3. Solve pychart's error unresolved reference 'selenium' unresolved reference 'webdriver‘

店铺排名问题,如何解决?
随机推荐
第一个ALV程序2
Who can elaborate on the semi consistent read under mysqlrc and how to reduce the deadlock probability?
Beijing post network research 2015 problem2
Why BGP server is used in sunflower remote control? Automatic optimal route and high-speed transmission across operators
Is the array name a pointer
Wechat applet monitors sliding events on the screen
Code ~ hide or disable the status bar and virtual keys
LDP --- 标签分发协议
小马智行进军前装量产,从自研域控制器入手?
How to understand clock cycle and formula CPU execution time = number of CPU clock cycles / dominant frequency
Const char* and char*, string constants
3.解决Pycharm报错Unresolved reference ‘selenium‘ Unresolved reference ‘webdriver‘
[introduction to C language] zzulioj 1031-1035
The const keyword of ES6 declares variables
大厂们终于无法忍受“加一秒”了,微软谷歌Meta等公司提议废除闰秒
SQL窗口函数
The function parameters of the new features of ES6 are assigned initial values and rest parameters
tron OUT_ OF_ ENERGY
Configmap configuration and secret encryption
Ribbon principle analysis namedcontextfactory