What do you mean by “Pointers to Function”? What are Function Pointers ?

October 28, 2007 · Filed Under C++ Questions, Placement Questions 



Don't want to miss a single bit? Subscribe By Email for Daily Jobs

Enter your email address :

All the latest from JobsAdda on Jobsadda.com delivered to your mailbox everyday.

We can use a function in two ways: one is to invoke a function and the second is to obtain the address of a function. This address that is obtained is called as pointer to the function and is used to invoke the function.
E.g. we have a function named display and we will invoke this function using a pointer to this function as follow:
void display (string s) /* … */
void (*ptr)(string); //pointer to a function

ptr = &display; //assign the address of the function
ptr (“hello”); // call the function

In the above example the compiler will discover that ptr is a pointer to the function display and calls the function. The dereferencing of a pointer to a function using a * is optional. Similarly it is not necessary to use & to get the address to the pointer.

As pointers to functions have argument types declared just like the functions themselves, therefore in pointer assignments the complete function type must exactly match. A function name does not constitute its type. A function type is determined by its return type and its parameter list

Related Articles


Get Latest JobsAdda.com Jobs, news & updates  via Email