第36课

第36课 - 函数与指针分析

1. 函数类型

(1)C语言中的函数有自己特定的类型,函数的类型由返回值参数类型参数个数共同决定

        比如,int add(int i, int j) 的类型为 int(int, int)

(3)C语言中通过typedef为函数类型重命名,typedef return_type name(parameter list)

        比如,typedef int f(int, int);       typedef void p(int);

2. 函数指针

(1)函数指针用于指向一个函数

(2)函数名是执行函数体的入口地址

(3)有三种定义函数指针的方法

        ① 首先使用typedef重命名函数类型,然后使用它定义函数指针变量,typedef  return_type FuncType (parameter list);    FuncType* pfunc;

              

        ② 直接定义,return_type (*pointer) (parameter list);  

      • pointer 是函数指针变量名       // pointer是一个指针变量       
      • return_type 是所指函数的返回值类型
      • parameter list 是所指函数的参数类型列表

             

        ③  使用typedef直接定义函数指针类型,然使用此类型定义函数指针变量,typedef  return_type (* PFuncType)(parameter list);   pFuncType pfunc; 

             

【函数指针的使用】

 1 #include <stdio.h>
 2 
 3 typedef int (FUNC)(int);    // 使用typedef重命名函数类型
 4 int test(int i)
 5 {
 6     return i * i;
 7 }
 8 
 9 void f()
10 {
11     printf("Call f()...
");
12 }
13 
14 int main()
15 {
16     FUNC* pt = test;    // 函数名就是函数体的入口地址
17 
18     // 前面学习数组时强调过,对于int a[10];这个数组而言,a和&a的值是相同的,但它们表示的意义不同
19     // 与数组不同,函数名与对函数名取址表示的意义是相同的,即下面的f和&f含义相同
20     void(*pf)() = &f;   // 老式编译器可能有这种写法;现代编译器直接使用函数名即可
21 
22     // 三者的意义相同
23     printf("pf = %p
",pf);   // pf = 0x40058d
24     printf("f = %p
",f);     // f = 0x40058d
25     printf("&f = %p
",&f);   // &f = 0x40058d
26 
27 
28     pf();   // 使用函数指针调用函数
29     
30     (*pf)();  // 老式写法
31 
32     printf("Function pointer call: %d
", pt(2));   // 使用函数指针调用函数
33 
34     return 0;
35 }

3. 回调函数

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

(2)回调机制原理

    • 调用者不知道具体事件发生时需要调用的具体函数
    • 被调函数不知道何时被调用,只知道需要完成的任务
    • 当具体事件发生时,调用者通过函数指针调用具体函数

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

 1 #include <stdio.h>
 2 
 3 typedef int (*Weapon)(int);
 4 
 5 // 使用回调函数动态切换装备
 6 void fight(Weapon wp,int arg)
 7 {
 8     int result = 0;
 9     
10     printf("Fight boss!
");
11     
12     result = wp(arg);
13 
14     printf("Boss loss:%d
",result);
15 }
16 
17 // 使用刀作为武器
18 int knife(int n)
19 {
20     int ret = 0;
21     int i = 0;
22 
23     for (i=0; i< n; i++)
24     {
25         printf("Knife attack:%d
",1);
26         ret++;
27     }
28 
29     printf("
");
30 
31     return ret;   
32 }
33 
34 // 使用剑作为武器
35 int sword(int n)
36 {
37     int ret = 0;
38     int i = 0;
39 
40     for (i=0; i< n; i++)
41     {
42         printf("Sword attack:%d
",5);
43         ret++;
44     }
45 
46     printf("
");
47 
48     return ret;   
49 }
50 
51 // 使用枪作为武器
52 int gun(int n)
53 {
54     int ret = 0;
55     int i = 0;
56 
57     for (i=0; i< n; i++)
58     {
59         printf("Gun attack:%d
",10);
60         ret++;
61     }
62 
63     printf("
");
64 
65     return ret;   
66 }
67 
68 int main()
69 {
70     fight(knife, 3);    // 用刀砍3次
71     fight(sword, 4);    // 用剑刺4次
72     fight(gun, 5);      // 开枪5次
73 
74     return 0;
75 }
76 
77 /*  程序运行结果
78     Fight boss!
79     Knife attack:1
80     Knife attack:1
81     Knife attack:1
82 
83     Boss loss:3
84     Fight boss!
85     Sword attack:5
86     Sword attack:5
87     Sword attack:5
88     Sword attack:5
89 
90     Boss loss:4
91     Fight boss!
92     Gun attack:10
93     Gun attack:10
94     Gun attack:10
95     Gun attack:10
96     Gun attack:10
97 
98     Boss loss:5
99 */
回调函数使用示例
原文地址:https://www.cnblogs.com/shiwenjie/p/11854010.html