C 中的回调
cserver side programmingprogramming更新于 2024/9/8 17:19:00
回调基本上是作为参数传递给其他代码的任何可执行代码,这些代码有望在给定时间回调或执行参数。我们可以用其他方式定义它:如果将函数的引用传递给另一个函数参数进行调用,则它被称为回调函数。
在 C 中,我们必须使用函数指针来调用回调函数。以下代码显示了回调函数如何执行其任务。
示例代码
#include<stdio.h> void my_function() { printf("This is a normal function."); } void my_callback_function(void (*ptr)()) { printf("This is callable function.
"); (*ptr)(); //调用回调函数 } main() { void (*ptr)() = &my_function; my_callback_function(ptr); }
输出
This is callable function. This is a normal function.