RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: C++
Expertise: Advanced
Oct 4, 2005



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Arrays of Functions

A pointer to a function refers to the memory address of the function. Just like arrays, the name of a function is the starting address of the function's code. Here's a function prototype that recieves a pointer to a function as one of its parameters:

void update( employee [], int, void (*)() );
This indicates that "update" will recieve a function as an argument that is referenced by a pointer.

Suppose you have a couple of functions defined for manipulating some array of type employee: ComputeSalary, GiveRaise, Demote, Promote, etc. This would be the ideal call to the update function, assuming that the array of employees name is roll and the employee you wish to modify is indexed by being the fourth element of this array and you wish to ComputerSalary:

update( roll, 4, ComputeSalary );
If you wanted to Demote:

update( roll, 4, Demote );
Now, further suppose that you've got three functions: A, B, and C. Each returns void and takes one integer argument. To declare an array of pointers to these three functions:

void (*AoP[3])(int) = {A, B, C};
To call the nth element (or function) of AoP with argument Z:

David Bandel
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date