Function pointer is a type of pointer which points a function or code block. You can assign address of a function to function pointer variable and that function using function pointer variable name.
I have a bad experience while making Calculator. In my design all the calculation takes place when user pressed equal button. When a user pressed equal button I need to check every time which operator user was pressed , checked by switch case, it become complicated then I increase more function in my calculator because of lots condition for operator check and attached code for particular logic separately.
Now my work becomes simple because of function pointer because I just need to check operator pressed by user and attached related function code to function pointer. It reduces lots of condition checking and assigns separate code to each section. So function pointer is very useful in some specific scenario.
Advantage of function pointer
- Implement Callback
- Useful in event handling
- One entry point for more than one code block