一个简单的回调函数的例子

时间:2022-06-26 19:32:05

#include <stdio.h>

typedef void( *T_SIMPLE_CALLBACK )( void );//简单的回调函数的类型定义

typedef unsigned int( *T_FULL_CALLBACK )( unsigned int paral ,unsigned int para2);//完整的回调函数的类型定义

void simple_callback( void )
{
printf("=====simple_callback====\n");
}

unsigned int full_callback( unsigned int para1 ,unsigned int para2)
{
printf("=====full_callback====\n");
printf("=====para1:%d,para2:%d====\n",para1,para2);

return para1+para2;
}

void test_simple_callback(T_SIMPLE_CALLBACK p_simple_callback)
{
printf("=====test_simple_callback begin====\n");

if(p_simple_callback)
{
(*p_simple_callback)();//简单回调函数被调用
}
else
{
printf("=====no callback====\n");
}

printf("=====test_simple_callback end====\n");
}

void test_full_callback(T_FULL_CALLBACK p_full_callback ,unsigned int para2)
{
unsigned int ret_callback;
unsigned int para1;
para1 = 100;

printf("=====test_full_callback begin====\n");

if(p_full_callback)
{
ret_callback = (*p_full_callback)(para1,para2);//完整回调函数被调用
printf("=====%d+%d=%d====\n",para1,para2,ret_callback);
}
else
{
printf("=====no callback====\n");
}

printf("=====test_full_callback end====\n");
}


int main(int argc,char **argv)
{
//T_SIMPLE_CALLBACK p_simple_callback = 0;
T_SIMPLE_CALLBACK p_simple_callback = simple_callback;
T_FULL_CALLBACKp_full_callback = full_callback;

printf("=====main begin====\n");

test_simple_callback(p_simple_callback);
test_full_callback(p_full_callback,200);

printf("=====main end====\n");

return 0;
}