pointers to functions in c

Chapter 8:Preprocessor And Complex Declarations

    pointers to functions in c

  •           A useful method is the ability to have pointers to functions. It is used to store the address of a function that can be called later using that function pointer. It offers a way of passing around instructions for how to perform something. It helps to write flexible functions and libraries. It can be declared, assigned values and then used to access the functions they point to. They are declared as follows:

              int fp(int a, float b);

              Then add () brackets around the function name and * ahead of it. It declares the pointer. Due to precedence, if we don't put (), we state that function returning a pointer.

    /* function returning pointer to int */

    int *fp(int a, float b);

    /* pointer to function returning int */

    int (*func)(int a, float b);

              After that we can assign the address of the right sort of function just by using its name. When function name is used in an expression, it is changed into an address like an array. Now we can call the function using any of the following forms:


    /* or */



    #include  < stdio.h >
    #include  < stdlib.h >
    void disp(int);
          void (*fp)(int);
          fp = disp;
    Void disp(int i)
          printf("%d\n", i);

© 2015 by Learncertification All Rights Reserved. The certification names are the trademarks of their respective owners. Terms & Privacy Policy