回调函数实例

/*
*回调函数就是一个通过函数指针调用的函数。
*如果你把函数的指针(地址)作为参数传递给另一个函数,
*当这个指针被用来调用其所指向的函数时,我们就说这是回调函数。
*/ 

#include <stdio.h>


//函数指针定义 
typedef void (*pcb)(int x, int y);


/* 
*回调函数 GetCallBackA
*/

void sum(int x, int y)
{
	int temp;
	temp = x + y;
	printf("x + y = %d \r\n", temp);
}

/*
*实现回调函数的调用函数 GetCallBackA
*/
void GetCallBackA(pcb callfun, int a, int b)
{
	printf("GetCallBackA Test:\r\n");
	callfun(a, b);
}

/*
*实现回调函数的调用函数回调函数 GetCallBackA
*/ 
void GetCallBackB(void(* callfun)(), int a, int b)
{
	printf("GetCallBackB Test:\r\n");
	callfun(a, b);
}

int main(int argc, char *argv[])
{
	GetCallBackA(sum, 4, 6);	
	GetCallBackB(sum, 4, 6);
	printf("Hello C-Free!\n");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/graduation201209/article/details/62037061