這是實驗樓上一個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; }
再寫一個實現回調的例子:debug
#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; }