zoukankan      html  css  js  c++  java
  • 函数指针实例三

    #include <stdio.h>
    /* 
                pf(void)            pf是一个无参数函数
              * pf(void)               pf是一个无参数函数,它的返回值是一个指针
            ( * pf(void) ) (void)      pf是一个无参数函数,它的返回值是一个无参数函数的指针
          * ( * pf(void) ) (void)       pf是一个无参数函数,它的返回值是一个无参数函数的指针,这个函数的返回值又是一个指针
        ( * ( * pf(void) ) (void) ) (void)  pf是一个无参数函数,它的返回值是一个无参数函数的指针,这个函数的返回值又是一个无参数函数的指针
    int ( * ( * pf(void) ) (void) ) (void)  pf是一个无参数函数,它的返回值是一个无参数函数的指针,这个函数的返回值又是一个无参数且返回值为int的函数的指针。 
    */
    
    /* 
            int    pf(void)            
            int *pf(void)      pf无参数函数,返回int*指针   
           int ( * pf(void) ) (void)    pf无参数函数,返回一个无参数函数的指针,该函数返回int类型
         int * ( * pf(void) ) (void)  pf无参数函数,返回值是无参数函数的指针,该函数又返回一个int*指针     
    int ( * ( * pf(void) ) (void) ) (void)   
    declare pf as function (void) returning pointer to function (void) returning pointer to function (void) returning int
    
    void ( * ( * pf(void) ) (void) ) (void) 
    declare pf as function (void) returning pointer to function (void) returning pointer to function (void) returning void
    
    */
    
    
    
    int myadd( int a, int b)
    {
        int z = a + b;
        return z;
    }
    int mysub(int a, int b)
    {
        int z = a - b;
        return z;
    }
    int mymul(int a, int b)
    {
        int z = a*b;
        return z;
    }
    int mydiv(int a, int b)
    {
        int z = a/b;
        return z;
    }
    
    //array of function pointers,
    int (* opfunctptr [ ] ) ( int x, int y) = { myadd, mysub, mymul, mydiv };
    
    typedef int (*calc)(int x, int y );
    
    //function returning the function pointer of type int (*calc)(int x, int y )
    calc retmathfunc(int index)
    {
        return opfunctptr[index];
    }
    
    int main(int argc, char* argv[])
    {
        int choice, p1, p2, res;
        int (*calculator)(int x, int y);
        printf("Type -1 to quit
    ");
        printf("Type 0 - add, 1 - sub, 2 - mul, 3 - div
    ");
        scanf("%d", &choice);
        
        while( choice != -1)
        {
            calculator = retmathfunc(choice); //returns function pointer
                
            printf("Param1
    ");
            scanf("%d", &p1);
            printf("Param2
    ");
            scanf("%d", &p2);
            
            res = calculator(p1, p2); //calling function pointer
            printf("res = %d
    ", res);
        
            printf("Type 0 - add, 1 - sub, 2 - mul, 3 - div
    ");
            scanf("%d", &choice);
        }
        
        
        return 0;
    }
  • 相关阅读:
    Software_programming_automation_selenium
    Software_programming_EnterpriseArch_ServiceWithSingleTonFactory
    web-bootstrap-button
    Software--C#--grammer_Delegate--Event
    Software_C#_grammer_Deletegate--Strategy
    Software--BigData--StreamingData
    线程死锁和递归锁
    同步锁Lock(互斥锁)
    GIL计算python 2 和 python 3 计算密集型
    什么是python的全局解释锁(GIL)
  • 原文地址:https://www.cnblogs.com/CodeWorkerLiMing/p/9622988.html
Copyright © 2011-2022 走看看