Suppose I have these three functions:
bool A();
bool B();
bool C();
How do I call one of these functions conditionally using a function pointer, and how do I declare the function pointer?
Suppose I have these three functions:
bool A();
bool B();
bool C();
How do I call one of these functions conditionally using a function pointer, and how do I declare the function pointer?
If you want to call one of those functions conditionally, you should consider using an array of function pointers. In this case you'd have 3 elements pointing to A, B, and C and you call one depending on the index to the array, such as funcArray0 for A.
Declare your function pointer like this:
You can declare the function pointer as follows:
Which says we are declaring a function pointer to a function which does not take anything and return a bool.
Next Assignment:
To call the function using the function pointer:
I usually use typedef to do it, but it may be overkill, if you do not have to use the function pointer too often..
initially define a function pointer array which takes a void and returns a void. assuming that your function is taking a void and returning a void.
now u can use this to create function pointer variables of such functions. like below:
now store the address of your functions in the three variables.
now you can call these function using function pointers as below:
The best way to read that is The ``Clockwise/Spiral Rule''
By David Anderson
http://c-faq.com/decl/spiral.anderson.html