Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use callback function in C language

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to use the callback function in C language, the content is concise and easy to understand, it can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

1 what is a callback function? First of all, what is a "callback"?

My understanding is that a piece of executable code is passed to other code as a parameter, and this code will be called for execution at some point, which is called a callback.

If the code is executed immediately, it is called a synchronous callback, and if executed later, it is called an asynchronous callback.

A callback function is a function that is called through a function pointer. If you pass the pointer (address) of a function to another function as an argument, when the pointer is used to call the function it points to, we say this is a callback function.

The callback function is not called directly by the implementer of the function, but by another party when a particular event or condition occurs and is used to respond to that event or condition.

2 Why do you use callback functions?

Because the caller can be separated from the callee, the caller does not care who the callee is. It only needs to know that there is a called function with a specific prototype and constraints.

In short, a callback function allows the user to pass the pointer of the method to be called as an argument to a function, so that the function can flexibly use different methods when dealing with similar events.

Int Callback () /

< 回调函数 { // TODO return 0; } int main() ///< 主函数 { // TODO Library(Callback); ///< 库函数通过函数指针进行回调 // TODO return 0; } 回调似乎只是函数间的调用,和普通函数调用没啥区别。 但仔细看,可以发现两者之间的一个关键的不同:在回调中,主程序把回调函数像参数一样传入库函数。 这样一来,只要我们改变传进库函数的参数,就可以实现不同的功能,这样有没有觉得很灵活?并且当库函数很复杂或者不可见的时候利用回调函数就显得十分优秀。 3 怎么使用回调函数?int Callback_1(int a) ///< 回调函数1 { printf("Hello, this is Callback_1: a = %d ", a); return 0; } int Callback_2(int b) ///< 回调函数2 { printf("Hello, this is Callback_2: b = %d ", b); return 0; } int Callback_3(int c) ///< 回调函数3 { printf("Hello, this is Callback_3: c = %d ", c); return 0; } int Handle(int x, int (*Callback)(int)) ///< 注意这里用到的函数指针定义 { Callback(x); } int main() { Handle(4, Callback_1); Handle(5, Callback_2); Handle(6, Callback_3); return 0; } 如上述代码:可以看到,Handle()函数里面的参数是一个指针,在main()函数里调用Handle()函数的时候,给它传入了函数Callback_1()/Callback_2()/Callback_3()的函数名,这时候的函数名就是对应函数的指针,也就是说,回调函数其实就是函数指针的一种用法。 4 回调函数实例(很有用) 一个GPRS模块联网的小项目,使用过的同学大概知道2G、4G、NB等模块要想实现无线联网功能都需要经历模块上电初始化、注册网络、查询网络信息质量、连接服务器等步骤,这里的的例子就是,利用一个状态机函数(根据不同状态依次调用不同实现方法的函数),通过回调函数的方式依次调用不同的函数,实现模块联网功能,如下: /********* 工作状态处理 *********/ typedef struct { uint8_t mStatus; uint8_t (* Funtion)(void); //函数指针的形式 } M26_WorkStatus_TypeDef; //M26的工作状态集合调用函数 /********************************************** ** >

M26 working state set function

* * /

M26_WorkStatus_TypeDef M26_WorkStatus_Tab [] =

{

{GPRS_NETWORK_CLOSE, M26_PWRKEY_Off}, / / module shutdown

{GPRS_NETWORK_OPEN, M26_PWRKEY_On}, / / the module starts up

{GPRS_NETWORK_Start, M26_Work_Init}, / / pin initialization

{GPRS_NETWORK_CONF, M26_NET_Config}, / AT instruction configuration

{GPRS_NETWORK_LINK_CTC, M26_LINK_CTC}, / / connect to the dispatch center

{GPRS_NETWORK_WAIT_CTC, M26_WAIT_CTC}, / / wait for the dispatch center to reply

{GPRS_NETWORK_LINK_FEM, M26_LINK_FEM}, / / connect the front machine

{GPRS_NETWORK_WAIT_FEM, M26_WAIT_FEM}, / / wait for the front machine to reply

{GPRS_NETWORK_COMM, M26_COMM}, / / working normally

{GPRS_NETWORK_WAIT_Sig, M26_WAIT_Sig}, / / wait for the signal to reply

{GPRS_NETWORK_GetSignal, M26_GetSignal}, / / get the signal value

{GPRS_NETWORK_RESTART, M26_RESET}, / / module restart

} / *

* * > M26 module working state machine, calling 12 functions in turn

* * /

Uint8_t M26_WorkStatus_Call (uint8_t Start)

{

Uint8_t I = 0

For (I = 0; I < 12; iTunes +)

{

If (Start = = M26roomWorkStatus _ Tab [I] .mStatus)

{

Return M26 workload Workstatus _ Tab [I] .Funtion ()

}

}

Return 0

}

The above is how to use the callback function in C language. have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report