Ask your own question, for FREE!
Computer Science 7 Online
OpenStudy (anonymous):

why do i need pointers to function ?

OpenStudy (anonymous):

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

OpenStudy (anonymous):

\[# 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.

OpenStudy (bruce1q):

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.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!