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?
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.
+ 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.
0
When u actually want to pass the function as an argument to another function.