zoukankan      html  css  js  c++  java
  • 指针系统学习6-指向函数的指针

    1.用函数指针变量调用函数

    可以用指针变量指向整型变量、字符串、数组,也可以指向一个函数。一个函数在编译时被分配给一个入口地址。这个函数的入口地址就称为函数的指针。

    //指向函数的指针
    
    
    /****************************/
    /* 比较a 和 b的大小,求大值 */
    /****************************/
    
    #include <stdio.h>
    
    #if(0)//预编译命令:if语句为0,则不用编译,为1则编译!   
        // c语言不允许嵌套注释,所以用此方法可以注释比较多内容
    void main()
    {
          int max(int, int);
          int a, b, c;
    
          scanf("%d %d", &a, &b);
          
          c = max(a, b);
          
          printf("a = %d, b = %d, max = %d
    
    ", a, b, c);
    }
    #endif
    
    int max(int x, int y)
    {
          int z;
          
          if( x > y )
          {
                z = x;
          }
          else
          {
                z = y;
          }
    
          return z;
    }
    
    #if(1)
    //将 main 函数改写为
    void main()
    {
          int max(int, int);
          int (*p)();//定义了一个函数指针(指向函数的指针)
          int a, b, c;
    
          p = max;
          scanf("%d %d", &a, &b);
          
          c = (*p)(a, b);
          
          printf("a = %d, b = %d, max = %d
    
    ", a, b, c);
    }
    
    #endif

    2.用指向函数的指针作函数参数:

    函数指针变量常用的用途之一是把指针作为参数传递到其他函数。

    前面介绍过,函数的参数可以是变量、指向变量的指针变量、数组名、指向数组的指针变量等。

    现在介绍指向函数的指针也可以作为参数,以实现函数地址的传递,这样就能够在被调用的函数中使用实参函数。

    // 实参函数名     f1                 f2
                    ↓                  ↓
    void  sub( int (*x1)(int),int (*x2)(int,int))
    {  int a,b,i,j;
         a=(*x1)(i);       /*调用f1函数*/
         b=(*x2)(i,j);/*调用f2函数*/
          …
     }

    它的原理可以简述如下:有一个函数(假设函数名为sub),它有两个形参(x1和x2),定义x1和x2为指向函数的指针变量。在调用函数sub时,实参为两个函数名f1和f2,给形参传递的是函数f1和f2的地址。这样在函数sub中就可以调用f1和f2函数了。

    实战演练:

      设一个函数process,在调用它的时候,每次实现不同的功能。(有点类似多态)

        输入a和b两个数,第一次调用process时找出a和b中大者,第二次找出其中小者,第三次求a与b之和。

    /***********************************************************/
    /*  设一个函数process,在调用它的时候,每次实现不同的功能。*/
    /*  输入a和b两个数,第一次调用process时找出a和b中大者,*/
    /*  第二次找出其中小者,第三次求a与b之和。               */
    /***********************************************************/
    
    #include <stdio.h>
    
    void main()
    {
          int max(int, int);            /* 函数声明 */
          int min(int, int);            /* 函数声明 */
          int add(int, int);            /* 函数声明 */
        
          void process( int, int, int(*fun)() );    /* 函数声明 */
          
          int a, b;
    
          printf("Endter a and b: ");
          scanf("%d %d", &a, &b);
          
          printf("max = ");
          process(a, b, max);
    
          printf("min = ");
          process(a, b, min);
    
          printf("sum = ");
          process(a, b, add);
    }
    
    int max(int x, int y)              /* 函数定义 */
    {
          int z;
          
          if( x > y )
          {
                z = x;
          }
          else
          {
                z = y;
          }
    
          return z;
    }
    
    int min(int x, int y)              /* 函数定义 */
    {
          int z;
    
          if( x < y )
          {
                z = x;
          }
          else
          {
                z = y;
          }
    
          return z;
    }
    
    int add(int x, int y)
    {
          int z;
          
          z = x + y;
          return z;
    }
    
    void process( int x, int y, int(*fun)() )    /* 函数定义 */ 
    {
          int result;
    
          result = (*fun)(x, y);
          printf("%d
    ", result);
    }
    View Code
  • 相关阅读:
    Anagram
    HDU 1205 吃糖果(鸽巢原理)
    Codeforces 1243D 0-1 MST(补图的连通图数量)
    Codeforces 1243C Tile Painting(素数)
    Codeforces 1243B2 Character Swap (Hard Version)
    Codeforces 1243B1 Character Swap (Easy Version)
    Codeforces 1243A Maximum Square
    Codeforces 1272E Nearest Opposite Parity(BFS)
    Codeforces 1272D Remove One Element
    Codeforces 1272C Yet Another Broken Keyboard
  • 原文地址:https://www.cnblogs.com/tianqizhi/p/10046791.html
Copyright © 2011-2022 走看看