How can I call a function using a function pointer?

前端 未结 16 1659
孤独总比滥情好
孤独总比滥情好 2020-12-02 17:24

Suppose I have these three functions:

bool A();
bool B();
bool C();

How do I call one of these functions conditionally using a function poi

16条回答
  •  孤城傲影
    2020-12-02 17:32

    Calling a function through a function pointer

    float add(int, float), result;
    
    int main()
    {
        float (*fp)(int, float);
        float result;
        fp = add;
        result = add(5, 10.9);    // Normal calling
        printf("%f\n\n", result);
    
        result = (*fp)(5, 10.9);  // Calling via a function pointer
        printf("%f\n\n", result);
    
        result = (fp)(5, 10.9);   // Calling via function pointer. The
                                  // indirection operator can be omitted
    
        printf("%f", result);
        getch();
    }
    
    float add(int a, float b)
    {
        return a+b;
    }
    

    >

    Output

    15.90000
    15.90000
    15.90000
    

提交回复
热议问题