回调函数

https://blog.csdn.net/zgrjkflmkyc/article/details/9198519

回调函数通俗的解释:

 普通函数:你所写的函数调用系统函数,你只管调用,不管实现。

 回调函数:系统调用你所写的函数,你只管实现,不管调用。

以下是使用C语言实现回调函数的一个例子:

代码:

#include <stdio.h>

void PrintNum(int n);
void ShowNum(int n, void (*ptr)());

void PrintMessage1();
void PrintMessage2();
void PrintMessage3();
void ShowMessage(void (*ptr)());

int main()
{
	ShowNum(11111, PrintNum);
	ShowNum(22222, PrintNum);
	ShowMessage(PrintMessage1);
	ShowMessage(PrintMessage2);
	ShowMessage(PrintMessage3);
}

void PrintNum(int n)
{
	printf("Test1 is called,the number is %d\n", n);
}

void ShowNum(int n, void (*ptr)())
{
	(*ptr)(n);
}

void PrintMessage1()
{
	printf("This is the message 1!\n");
}

void PrintMessage2()
{
	printf("This is the message 2!\n");
}

void PrintMessage3()
{
	printf("This is the message 3!\n");
}

void ShowMessage(void (*ptr)())
{
	(*ptr)();
}

posted on 2021-12-16 14:52  夏天师妹  阅读(118)  评论(0编辑  收藏  举报

导航