Need of function pointer | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Need of function pointer

Why do I need to use pointer to a function, where I can call the function directly by its name?

22nd Feb 2020, 8:39 AM
Dipu
Dipu - avatar
3 Answers
+ 5
For example, it is used as parameters of some processing functions. standard librariry function qsort declared as follows: void qsort (void* base, size_t num, size_t size, int (*compar)(const void*,const void*)); where compar is a pointer to user function. Whithout it qsort doen't know how to compare array elements.
22nd Feb 2020, 8:51 AM
andriy kan
andriy kan - avatar
+ 3
I once had to program a state machine. It was a table clock emulator, where you would push a button and enter diferent states: you could see date, set date, set alarm, and so on. And instead of having a big switch/case and calling diferent functions, I had an array of function pointers and I would just call function_array[current_state] 😊 I'm grateful for function pointers.
22nd Feb 2020, 9:27 AM
Selin Genkur
0
When u actually want to pass the function as an argument to another function.
2nd May 2020, 3:12 PM
Lakshmi Priya Althuru
Lakshmi Priya Althuru - avatar