zoukankan      html  css  js  c++  java
  • 函数指针的定义与使用

    函数的本质

    函数的本质是一段可执行性代码段。函数名,则是指向这段代码段的首地址。

    #include <stdio.h>
    
    
    void print()
    {
        printf("china
    ");
    }
    void dis()
    {
        printf("china
    ");
    } 
    
    int main()
    {
        void (*pf)() = print; //void (*pf)() = &print; //此两种形式的的赋值方式都是没有问题的。
        pf(); //(*pf)();
        pf = dis;
        pf();
        return 0;
    }

    1)上述代码中函数void print()和void dis()他们都有一个公共的类型就是void (*)(void);

    2)使用void (*)(void)类型定义了一个变量名为p的函数指针,并且对其赋值;

    更简洁的表示方式

    当函数类型较为复杂时,我们往往使用typedef来定义一个简洁的类型名称使之更容易使用。

    例如:typedef void (*PFUNC)();这时候我们就可以这样去使用:

    typedef void (*PFUNC)() ;
    
    int main()
    {
        PFUNC pf= print;
        pf();
        pf = dis;
        pf();
        return 0;
    }

    应用场景

    函数指针的一个用法出现在菜单驱动系统中。例如程序可以提示用户输入一个整数
    值来选择菜单中的一个选项。 用户的选择可以做函数指针数组的下标, 而数组中的
    指针可以用来调用函数。

    #include <stdio.h>
    void function0(int);
    void function1(int);
    void function2(int);
    
    int main()
    {
    
        void (*f[3])(int) = {function0,function1,function2};
        //将这 3 个函数指针保存在数组 f 中
        int choice;
        printf("Enter a number between 0 and 2, 3 to end: ");
        scanf("%d",&choice);
        while ((choice >= 0) && (choice <3))
        {
            (f[choice])(choice);//(*f[choice])(choice);这种写法亦可
            //f[choice]选择在数组中位置为 choice 的指针。
            //指针被解除引用, 以调用函数, 并且 choice 作为实参传递给这个函数。
            printf("Enter a number between 0 and 2,3 to end: ");
            scanf("%d",&choice);
        }
        printf("Program execution completed.");
        return 0;
    }
    
    void function0(int a)
    {
        printf("You entered %d so function0 was called
    ",a);
    }
    
    void function1(int b)
    {
        printf("You entered %d so function1 was called
    ",b);
    }
    
    void function2(int c)
    {
        printf("You entered %d so function2 was called
    ",c);
    }

    回调函数

    #include <stdio.h>
    
    int callBackCompare(int a,int b)
    {
        return a<b?1:0;
    } 
    
    void selectSort(int *p, int n,int(*pf)(int,int))
    {
        for(int i=0; i<n-1 ;i ++)
        {
            for(int j=i+1; j<n; j++)
            {
                if(pf(p[i],p[j]))
                {
                    p[i] = p[i]^p[j];
                    p[j] = p[i]^p[j];
                    p[i] = p[i]^p[j];
                }
            }
        }
    } 
    int main(void)
    {
        int arr[10] = {6,5,4,3,2,1,7,8,9,0};
        selectSort(arr,10,callBackCompare);
        for(int i=0; i<10; i++)
        {
            printf("%d
    ",arr[i]);
        }
        return 0;
    }

    回调函数本质
    回调函数,本质也是一种函数调用,先将函数以指针的方式传入,然后,调用。这种写
    法的好处是,对外提供函数类型,而不是函数定义。这样我们只需要依据函数类型
    和函数功能提供函数就可以了。给程序的书写带来了很大的自由。

     

    ————雁过留痕,风过留声,人的记忆是一种很不靠谱的东西。记下这些笔记,希望自己能够在需要的时候有所回忆,也希望能够帮助哪些需要获取这些知识的人。
  • 相关阅读:
    前端PC人脸识别登录
    html2canvas 轮播保存每个图片内容
    基于Element的下拉框,多选框的封装
    聊聊 HTTPS
    从 rails 窥探 web 全栈开发(零)
    理解 Angular 服务
    Vue3 与依赖注入
    一次 HTTP 请求就需要一次 TCP 连接吗?
    GO 语言入门(一)
    读 Angular 代码风格指南
  • 原文地址:https://www.cnblogs.com/wangkeqin/p/9187735.html
Copyright © 2011-2022 走看看