Pointers To Functions

Introduction

  • A function pointer is a pointer that denotes a function rather than an object. Like any other pointer, a function pointer points to a particular type.
  • A function's type is determined by its return type and its parameter list. A function's name is not part of its type.
  • The signature (parameters and return value) of the function pointer must match the signature of the function

Example

// pf points to function returning bool that takes two const string references
     bool (*pf)(const string &, const string &);
 
// declares a function named pf that returns a bool*
     bool *pf(const string &, const string &);

Calling a function using a function pointer.

Two ways:

1. Explicit dereference

int foo(int nX) {}
int (*pFoo)(int) = foo; //assign pFoo to foo()
(*pFoo)(nValue); // call foo(nValue)

2. Implicit Dereference

int foo(int nX) {}
int (*pFoo)(int) = foo; //assign pFoo to foo()
pFoo(nValue); // call function foo(nValue) through pFoo.

Using Typedefs to Simplify Function Pointer Definitions

Function pointer types can quickly become unwieldy. We can make function pointers easier to use by defining a synonym for the pointer type using a typedef
Example
This definition says that cmpFcn is the name of a type that is a pointer to function. That pointer has the type "pointer to a function that returns a bool and takes two references to const string." When we need to use this function pointer type, we can do so by using cmpFcn, rather than having to write the full type definition each time.

     typedef bool (*cmpFcn)(const string &, const string &);

Examples

Example

int (*pFoo1) (); // pFoo1 is a pointer to a function that takes no arguments and returns an integer
int *pFoo2(); // pFoo2 is a function that takes no parameters and returns a pointer to an integer.

** Example 2**

#include <algorithm> // for swap
 
// Note our user-defined comparison is the third parameter
void SelectionSort(int *anArray, int nSize, bool (*pComparison)(int, int))
{
    using namespace std;
    for (int nStartIndex= 0; nStartIndex < nSize; nStartIndex++)
    {
        int nBestIndex = nStartIndex;
 
        // Search through every element starting at nStartIndex+1
        for (int nCurrentIndex = nStartIndex + 1; nCurrentIndex < nSize; nCurrentIndex++)
        {
            // Note that we are using the user-defined comparison here
            if (pComparison(anArray[nCurrentIndex], anArray[nBestIndex])) // COMPARISON DONE HERE
                nBestIndex = nCurrentIndex;
        }
 
        // Swap our start element with our best element
        swap(anArray[nStartIndex], anArray[nBestIndex]);
    }
}
 
// Here is a comparison function that sorts in ascending order
// (Note: it's exactly the same as the previous Ascending() function)
bool Ascending(int nX, int nY)
{
    return nY > nX;
}
 
// Here is a comparison function that sorts in descending order
bool Descending(int nX, int nY)
{
    return nY < nX;
}
 
// This function prints out the values in the array
void PrintArray(int *pArray, int nSize)
{
    for (int iii=0; iii < nSize; iii++)
        cout << pArray[iii] << " ";
    cout << endl;
}
 
int main()
{
    using namespace std;
 
    int anArray[9] = { 3, 7, 9, 5, 6, 1, 8, 2, 4 };
 
    // Sort the array in descending order using the Descending() function
    SelectionSort(anArray, 9, Descending);
    PrintArray(anArray, 9);
 
    // Sort the array in ascending order using the Ascending() function
    SelectionSort(anArray, 9, Ascending);
    PrintArray(anArray, 9);
 
    return 0;
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.