zoukankan      html  css  js  c++  java
  • C/C++

      这是实验楼上一个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
    ", type);
        return 0;
    }
    
    int alarm2(int type)
    {
        printf("two:%d
    ", type);
        return 0;
    }
    
    int alarm3(int type)
    {
        printf("three:%d
    ", 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
    ", (int*)param);
        return 0;
    }
    
    int main
    {
        // 自定义函数通过作为函数参数进行回调
        func_register(my_func, (void*)5);
        return 0;
    }
    

      

      

  • 相关阅读:
    9. Palindrome Number
    7. Reverse Integer
    650. 2 Keys Keyboard
    646. Maximum Length of Pair Chain
    523. Continuous Subarray Sum
    516. Longest Palindromic Subsequence
    dp问题解题思路
    494. Target Sum
    小波变换网文精粹:小波:看森林,也看树木(一)
    数学、海豚和花朵
  • 原文地址:https://www.cnblogs.com/darkchii/p/8977140.html
Copyright © 2011-2022 走看看