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

回调函数

程序员文章站 2022-07-09 18:33:27
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 PrintMessage... ......
 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 }