C语言回调函数

 1 #include <stdio.h>  
 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 }  

 

posted @ 2018-01-15 17:09  魂~  阅读(269)  评论(0编辑  收藏  举报