欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C语言回调函数

程序员文章站 2022-04-15 21:25:54
1 #include 2 3 void printnum1(int n); 4 void printnum2(int n); 5 void shownum(int n, void (* ptr)(...
1 #include

2

3 void printnum1(int n);

4 void printnum2(int n);

5 void shownum(int n, void (* ptr)(int));

6

7 void printmessage1();

8 void printmessage2();

9 void printmessage3();

10 void showmessage(void (* ptr)());

11

12 int main(){

13 shownum(11111, printnum1);

14 shownum(22222, printnum2);

15 showmessage(printmessage1);

16 showmessage(printmessage2);

17 showmessage(printmessage3);

18 }

19

20 void printnum1(int n){

21 printf("test1 is called,the number is %d\n", n);

22 }

23

24 void printnum2(int n){

25 printf("test2 is called,the number is %d\n", n);

26 }

27

28 void shownum(int n, void (* ptr)()){

29 (* ptr)(n);

30 }

31

32

33 void printmessage1(){

34 printf("this is the message 1!\n");

35 }

36

37 void printmessage2(){

38 printf("this is the message 2!\n");

39 }

40

41 void printmessage3(){

42 printf("this is the message 3!\n");

43 }

44

45 void showmessage(void (* ptr)()){

46 (* ptr)();

47 }