码迷,mamicode.com
首页 > 编程语言 > 详细

C/C++ - CallBack

时间:2018-05-01 23:21:02      阅读:164      评论:0      收藏:0      [点我收藏+]

标签: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;
}

  

  

C/C++ - CallBack

标签:debug   本地   main   ack   reg   提交   自定义函数   typedef   AC   

原文地址:https://www.cnblogs.com/darkchii/p/8977140.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!