what is the use and advantage of using function pointer

  1. profile image46
    rkvermaposted 7 years ago

    what is the use and advantage of using function pointer

  2. Nilam Dhok profile image60
    Nilam Dhokposted 6 years ago

    Function Pointer is a pointer which points to a function with different names but similar type of parameters. The Functions which performs different operations but still having same parameters as each other at that time function pointer are very useful for accessing functions in a flexible manner.

    For e.g.

    If there is one function called
    addition(int a, int b); & another function RectArea(int length, int width);
    These both functions are performing different tasks but still have same parameters.

    Now just we have to careful how the Function Pointer will know to call which function

    It is done by

    void (*function_pointer) (int, int)  // Function pointer Declaration
    function_pointer = &function_name;   // assigning Address of a Function
    (*funcation_pointer) (num1,num2); // call to an assinged function.

    As per above example
    void (*fptr) (int, int);

    fptr = &addition;   //  Call to Addition Function
    (*fptr) (a,b);


    fptr = & RectArea;     // Call to Area of Rectangle Function.
    (*fptr) (length, width);

 
working