I have a series of functions with same prototype and those are called from a switch/case at the moment.
I was asked to replace this with function pointers. The idea is to speed up the call.
Any help is greatly appreciated.
Shane
Function pointers...
Re: Function pointers...
First you need to define a type for the function prototype.
For example:
The above function type defines a void function with no parameters.
Ex:
Now you need to define the function array as below.
You can call the functions as below by given index.
Hope this will help.
For example:
Code: Select all
typedef void (*myFuncType) ();
Ex:
Code: Select all
void myFunction1()
{
}
Code: Select all
/* Function pointer array */
const myFuncType myFuncArray[NUMBER_OF_FUNCTIONS] = {
myFunction0,
myFunction1,
myFunction2,
myFunction3,
myFunction4,
.....
.....
myFunctionn
};
Code: Select all
myFuncArray[3](); // this will call 'myFunction3'
Re: Function pointers...
Comprehensive explanation. Worth a donation