|
|
We can now invoke (call) this function via the pointer
But what are they really good for?
1. Passing a function as a parameter
By this means different operations can be done with the
same piece of code
2. To select a function to call at run-time, so avoiding
ugly (or impractical) switch statement.
For example, we want to read a key press and call a
different action (function) depending on the key selected.
For this we define an array of function pointers as follows:
for ( i = 'a'; i <= 'z'; i++)
RespondToKeypress[i] = alpha_handler;
So now when some key c is pressed we invoke
and the job is done
|
|