Switch statement Vs function pointers

Are there any advantages of using an array of function pointers instead of switch statements with the function calls within them?
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

KurtVonConnect With a Mentor Commented:
Um, let's see.

1. It's much faster.

2. You can change what values map to what functions on the fly.

3. It makes more compact code.

And why you shouldn't:

1. It's not very flexible (all functions must have the same set of parameters).

2. It's not very readable.

3. Initization of the array can be a real pain.

Personally, reason 2 is the only thing that would convince me to do it.  Hope that helps.
I'd agree with Kurt, except that unless you are making heavy use and calling ALOT of functions, the speed you gain with an array of function pointers isn't going to be seen...

I always take readability/maintainability over code compactness...  Just comes from working on larger projects with more than 1 person involved...
The array of function pointers is going to be a tad faster, but it rarely matters if the function is more than a statement or two.

The downside is if you forget to initialize one of the pointers, or if your index goes outside the array bounds, BOOM!

I would use a switch statement, unless the number of functions got to be more than 15 or so, then the function array would be less visual clutter, although harder to declare and initialize.

The Lifecycle Approach to Managing Security Policy

Managing application connectivity and security policies can be achieved more effectively when following a framework that automates repeatable processes and ensures that the right activities are performed in the right order.

arutAuthor Commented:
How is it faster?

I need to do a lookup anyway!
arutAuthor Commented:
Sorry, I had a table lookup in mind.

Is it faster because you get direct access here
and in the case of a switch one has to make a comparison
with each case before executing a particular case statement?
It is faster because you have direct access to the function pointer, there are no comparisons to be made at all. Obviously the number of functions you have will affect the average number of comparisons that will be made, and thus the speed issue becomes more important as the number of functions increases.

A switch statement is clearer and more readable except when the number of functions is high. I don't see declaration and initialisation of an array to be that hard. You can use variable argument lists with your functions so the number of parameters passed to them can be flexible but a switch statement would be easier to read and code.

You can pass an array of pointers to a function, you can't do that with a switch statement.
KurtVon answer is excelent. I just add two cases where function pointers is a MUST and switch is useless.
1. When the number of functions is dynamic, always changing at run time (You can't rewrite switch ot the fly, can't you ?). Try to build a state machine editor, and you will know what I mean.

2. When the priority of the functions is dynamic. With function pointer, you can move the high priority function to the front and will be checked earlier. You can't change the switch test order at run time. This is important on rel-time application.


arutAuthor Commented:
Actually if there is a way to share points, I would have given points to Kocil too. He gave some good examples!
All Courses

From novice to tech pro — start learning today.