标签:debug 本地 main ack reg 提交 自定义函数 typedef AC
这是实验楼上一个callback debug例子,我没有提交验证,但在本地上运行没有任何问题,也无警告:
#include <stdio.h>
#define MAX 3
typedef int (*alarm) (int type);
alarm alarm_list[MAX];
int index = 0;
void register_alarm(alarm a);
int hit_alarm(int index);
void register_alarm(alarm a)
{
alarm_list[index++] = a;
}
int hit_alarm(int index)
{
if (index < 0 || index >= MAX)
return 1;
(*alarm_list[index]) (index);
return 0;
}
int alarm1(int type)
{
printf("one:%d\n", type);
return 0;
}
int alarm2(int type)
{
printf("two:%d\n", type);
return 0;
}
int alarm3(int type)
{
printf("three:%d\n", type);
return 0;
}
int main
{
register_alarm(alarm1);
register_alarm(alarm2);
register_alarm(alarm3);
hit_alarm(0);
hit_alarm(1);
hit_alarm(2);
return 0;
}
再写一个实现回调的例子:
#include<stdio.h>
typedef int (*__callback__) (void* type_param);
void func_register(__callback__ func, void* param);
void func_register(__callback__ func, void* param)
{
(*func) (param);
}
// 自定义函数
int my_func(void* param)
{
printf("Param is %d\n", (int*)param);
return 0;
}
int main
{
// 自定义函数通过作为函数参数进行回调
func_register(my_func, (void*)5);
return 0;
}
标签:debug 本地 main ack reg 提交 自定义函数 typedef AC
原文地址:https://www.cnblogs.com/darkchii/p/8977140.html