• C--函数指针和回调函数


    时间会过去,只有记忆不会
    函数指针和回调函数

    指针函数
    在程序编译的时候会为函数分配一个入口地址,这个地址也就称为函数的指针,函数名代表函数的入口地址。

    typedef int(p)(int,int);
    
    void my_func(int a,int b){
    	printf("%d %d",a,b);
    }
    
    void test()
    {
    	p* p=my_func();
    	p2(10,20);	
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    指针函数:指向函数的指针

    • 函数指针定义方式(先定义函数类型,根据类型定义指针变量);
    • 先定义函数指针类型,根据类型定义指针变量;
    • 直接定义函数指针变量;
    int my_func(int a,int b)
    {
    	printf("ret:%d\n",a+b);
    	return 0;
    }
    
    //1.先定义函数类型,通过类型定义指针
    void test01()
    {
    	typedef int(FUNC_TYPE)(int,int);
    	FUNC_TYPE* f = my_func;
    	//如何调用?
    	(*f)(10,20);
    	f(10,20);
    }
    
    //定义函数指针类型
    void test02()
    {
    	typedef int(*FUNC_POINTER)(int,int);
    	FUNC_POINTER f = my_func;
    	//如何调用?
    	(*f)(10,20);
    	f(10,20);
    }
    
    //直接定义函数指针变量
    void test03()
    {
    	int(*f)(int,int) = my_func;
    	//如何调用?
    	(*f)(10,20);
    	f(10,20);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    函数指针数组:每个元素都是函数指针

    void func01(int a)
    {
    	printf("func01:%d\n",a);
    }
    void func02(int a)
    {
    	printf("func01:%d\n",a);
    }
    void func03(int a)
    {
    	printf("func01:%d\n",a);
    }
    
    void test()
    {
    #if 0
    	//定义函数指针
    	void(*func_array[])(int) = {func01, func02,func03};
    #else
    	void (*func_array[3])(int);
    	func_array[0] = func01;
    	func_array[1] = func02;
    	func_array[2] = func03
    #endif
    	for(int i=0;i<3;i++)
    	{
    		func_array[i](10+i);
    		(*func_array[i](10+i));
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    回调函数:就是相当于函数指针做函数参数

    void fun(int*p)(int a))
    
    • 1
    int plus(int a, int b)
    {
    	return a + b;
    }
    int sub(int a, int b)
    {
    	return a - b;
    }
    int mul(int a, int b)
    {
    	return a * b;
    }
    int division(int a, int b)
    {
    	return a / b;
    }
    
    void Calculator(int (*myCalculate)(int,int),int a,int b)
    {
    	int ret = myCalculate(a,b);
    	printf("ret = %d\n",ret);
    }
    
    void test()
    {
    	int num1=10;
    	int num2=20;
    	Calculator(plus, num1, num2);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    函数指针是指向函数的指针;
    指针函数是返回类型为指针的函数;

  • 相关阅读:
    金融工程学学习笔记第一章
    21天学习挑战赛之java集合
    Linux 之nmcli网络配置命令
    【必看】Midjourney订阅前必看的十件事
    厌倦了安装数据库?改用 Docker
    JavaSE——数字格式化、产生随机数字、生成验证码
    带上ES一起寻找理想的另一半
    机器人动力学模型与MATLAB仿真
    电脑黑屏按什么键恢复?只需要3个键就可以解决黑屏
    5分钟学会canvas的使用
  • 原文地址:https://blog.csdn.net/simple_core/article/details/126718933