How can I use an array of function pointers?

前端 未结 10 1146
别那么骄傲
别那么骄傲 2020-11-22 17:24

How should I use array of function pointers in C?

How can I initialize them?

10条回答
  •  花落未央
    2020-11-22 17:44

    Here's how you can use it:

    New_Fun.h

    #ifndef NEW_FUN_H_
    #define NEW_FUN_H_
    
    #include 
    
    typedef int speed;
    speed fun(int x);
    
    enum fp {
        f1, f2, f3, f4, f5
    };
    
    void F1();
    void F2();
    void F3();
    void F4();
    void F5();
    #endif
    

    New_Fun.c

    #include "New_Fun.h"
    
    speed fun(int x)
    {
        int Vel;
        Vel = x;
        return Vel;
    }
    
    void F1()
    {
        printf("From F1\n");
    }
    
    void F2()
    {
        printf("From F2\n");
    }
    
    void F3()
    {
        printf("From F3\n");
    }
    
    void F4()
    {
        printf("From F4\n");
    }
    
    void F5()
    {
        printf("From F5\n");
    }
    

    Main.c

    #include 
    #include "New_Fun.h"
    
    int main()
    {
        int (*F_P)(int y);
        void (*F_A[5])() = { F1, F2, F3, F4, F5 };    // if it is int the pointer incompatible is bound to happen
        int xyz, i;
    
        printf("Hello Function Pointer!\n");
        F_P = fun;
        xyz = F_P(5);
        printf("The Value is %d\n", xyz);
        //(*F_A[5]) = { F1, F2, F3, F4, F5 };
        for (i = 0; i < 5; i++)
        {
            F_A[i]();
        }
        printf("\n\n");
        F_A[f1]();
        F_A[f2]();
        F_A[f3]();
        F_A[f4]();
        return 0;
    }
    

    I hope this helps in understanding Function Pointer.

提交回复
热议问题