#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
typedef void (*ota_start_fun)(int *num1,int *num2);
typedef void (*ota_stop_fun)(int *num1,int *num2);
typedef struct callback_ctl{
ota_start_fun ota_start;
ota_stop_fun ota_stop;
}callback_ctl_t;
static void ota_start(int *num1,int *num2);
static void ota_stop(int *num1,int *num2);
static void ota_start(int *num1,int *num2){
int num3;
num3 = *num1 + *num2;
printf("~~~~~%d + %d = %d\n",*num1,*num2,num3);
}
callback_ctl_t ota;
static void ota1(ota_start_fun ota_start_cb,ota_start_fun ota_stop_cb){
ota.ota_start = ota_start_cb;
ota.ota_stop = ota_stop_cb;
}
//回调函数体
static void ota_stop(int *num1,int *num2){
int num3;
num3 = *num1- *num2;
printf("~~~~~%d + %d = %d\n",*num1,*num2,num3);
}
int main(int argc, char *argv[]) {
int num1=5;
int num2=3;
ota1(ota_start,ota_stop); //初始化回调函数,即将回调函数注册一下,放入一个全局变量。
ota.ota_start(&num1,&num2); //然后我就可以用这个结构体里面的这个回调函数变量来调用回调函数,并且传入参数。
ota.ota_stop(&num1,&num2);
return 0;
}