why do i need pointers to function ?
Among other uses, if you need to pass a function to a function as an argument, then function pointers are the only way to go
\[# typedef void(*sorting_algorithm)(int*, size_t); void test_sorting_algorithm(int* A, const size_t N, sorting_algorithm sort, const char* sort_name) { printf("Before %s: ", sort_name); print_array(A, N); sort(A, N); printf("After %s: ", sort_name); print_array(A, N); }\] in the example above, sorting_algorithm (and subsequently sort in the test_sorting_algorithm() parameter list) is a pointer to a function, that takes a pointer to int and a size_t parameter, and returns void. If I want to invoke different runtime behavior depending on what sorting algorithm I decide to pick, this is one way to do it.
In general, function pointers allow a function to call another function that is unknown to it at compile time. You can pass a function pointer to some code and the function you pass can be called under certain conditions.
Join our real-time social learning platform and learn together with your friends!