第36课 函数与指针分析

1. 函数类型

(1)C语言中的函数有自己特定的类型,这个类型由返回值、参数类型参数个数共同决定。如int add(int i,int j)的类型为int(int,int)。

(2)C语言中通过typedef函数类型重命名

    •  typedef type name(parameter list);  //如typedef int f(int,int);

 

2. 函数指针

(1)函数指针用于指向一个函数函数名执行函数体入口地址

(2)定义函数指针的两种方法

①通过函数类型定义:FuncType* pointer;

②直接定义:type(*pointer)(parameter list);       //其中type为返回值类型pointer函数指针变量名,parameter list为参数类型列表

【实例分析】函数指针的使用(技巧:使用函数指针直接跳转到某个固定的地址开始执行) 

 1 #include <stdio.h>
 2 
 3  
 4 
 5 typedef int (FUNC)(int);
 6 
 7 int test(int i)
 8 
 9 {
10 
11     return i * i;
12 
13 }
14 
15  
16 
17 void f()
18 
19 {
20 
21     printf("Call f()...
");
22 
23 }
24 
25  
26 
27 int main()
28 
29 {
30 
31     FUNC* pt = test; //合法,函数名就是函数体的入口地址
32 
33  
34 
35     //直接定义函数指针,&f是旧式写法。函数名只是一个符号(不是变量),
36 
37     //与数组名一样,并不为其分配内存,因此&f和f在数值上是相等的。
38 
39     void(*pf)() = &f; //如果知道某个函数的地址,这里可以改为一个固定的地址值,实现跳转!
40 
41  
42 
43     printf("pf = %p
",pf);
44 
45     printf("f = %p
",f);
46 
47     printf("&f = %p
",&f); //结果应为:pf == f == &f;
48 
49                
50 
51     pf();//利用函数指针调用
52 
53    
54 
55     (*pf)(); //旧式写法
56 
57  
58 
59     printf("Function pointer call:%d
",pt(2));
60 
61  
62 
63     return 0;
64 
65 }

3. 回调函数

(1)回调函数是利用函数指针实现的一种调用机制

(2)回调机制原理

  ①调用者不知道具体事件发生时需要调用的具体函数

  ②被调函数不知道何时被调用,只知道需要完成的任务

  ③当具体事件发生时,调用者通过函数指针调用具体函数

(3)回调机制中的调用者被调用者互不依赖

【实例分析】回调函数使用示例

  1 #include <stdio.h>
  2 
  3  
  4 
  5 typedef int (*Weapon)(int); //操作某种武器的函数
  6 
  7  
  8 
  9 //使用某种武器与boss进行战斗
 10 
 11 void fight(Weapon wp,int arg) //arg为传给函数指针的参数
 12 
 13 {
 14 
 15     int result = 0;
 16 
 17    
 18 
 19     printf("Fight boss!
");
 20 
 21    
 22 
 23     result = wp(arg);//调用回调函数,并传入参数arg
 24 
 25  
 26 
 27     printf("Boss loss:%d
",result);//Boss失血多少?
 28 
 29 }
 30 
 31  
 32 
 33 //使用武器——刀
 34 
 35 int knife(int n)
 36 
 37 {
 38 
 39     int ret = 0;
 40 
 41     int i = 0;
 42 
 43  
 44 
 45     for (i=0; i< n; i++)
 46 
 47     {
 48 
 49         printf("Knife attack:%d
",1);
 50 
 51         ret++;
 52 
 53     }
 54 
 55  
 56 
 57     printf("
");
 58 
 59  
 60 
 61     return ret;  
 62 
 63 }
 64 
 65  
 66 
 67 //使用武器——剑
 68 
 69 int sword(int n)
 70 
 71 {
 72 
 73     int ret = 0;
 74 
 75     int i = 0;
 76 
 77  
 78 
 79     for (i=0; i< n; i++)
 80 
 81     {
 82 
 83         printf("Sword attack:%d
",5);
 84 
 85         ret++;
 86 
 87     }
 88 
 89  
 90 
 91     printf("
");
 92 
 93  
 94 
 95     return ret;  
 96 
 97 }
 98 
 99  
100 
101 //使用武器——枪
102 
103 int gun(int n)
104 
105 {
106 
107     int ret = 0;
108 
109     int i = 0;
110 
111  
112 
113     for (i=0; i< n; i++)
114 
115     {
116 
117         printf("Gun attack:%d
",10);
118 
119         ret++;
120 
121     }
122 
123  
124 
125     printf("
");
126 
127  
128 
129     return ret;  
130 
131 }
132 
133  
134 
135 int main()
136 
137 {
138 
139  
140 
141     fight(knife, 3);//用刀砍3次
142 
143     fight(sword, 4);//用剑刺4次
144 
145     fight(gun, 5);  //开枪5次
146 
147  
148 
149     return 0;
150 
151 }

4. 小结

(1)C语言中的函数都有特定的类型

(2)可以使用函数类型定义函数指针

(3)函数指针是实现回调机制关键技术

(4)通过函数指针以在C程序中实现固定地址跳转