views:

73

answers:

4

How is that function pointer better than if-else or switch case? Is it because function pointer helps callback functions and thus promotes asynchronous implementation?

+1  A: 

It's better if the functions are not known beforehand. How would you design the C standard library qsort() without using a function pointer?

+1  A: 

It's not "better", it's different. Yes, the main purpose of function pointer is to provide callback functionality. No, it is not directly related to asynchronism.

Check this article for more information on function pointers.

Igor Krivokon
+1  A: 

According to Nigel Jones, it improves code readability, among others

http://embeddedgurus.com/stack-overflow/2010/04/efficient-c-tip-12-be-wary-of-switch-statements/

He even provides some examples:

http://www.rmbconsulting.us/Publications/jump-Tables.pdf

eee
A: 

Function pointers result in a better design of your code. Consider when you have an option of calling one function out of fifty functions. How massive would the switch case be? However, you could easily map all the function pointers as per their id, and call the appropriate function from the map using the "id". The code would be easily maintained and look neat.

There are other benefits and powers of using function pointer. For a good explanation, look at the tutorial here

mots_g