Overview

Why use pointers?

What's a pointer?

Declaring pointers

Initializing/Setting pointers

Pointer operators

Pointer arithmetic

Pointers as parameters

Pointers vs. arrays

Array of pointers

Function pointers

 


Functions pointers

  • a pointer to a function contains the address of the function in memory
  • a function name, the same like array name, the starting address in memory of the code that performs the function's task.
  • pointers to functions can be passed to functions , returned from functions, stored in arrays, and assigned to other function pointers.
  • one use of functin pointers is in menu-driven systems

Example:


void func1(int);
void func2(int);

main()
{
    int set=0;
    void (*foo[])(int name) = {func1, func2};

    do {

       cout << "Enter a number (0 or 1): ";

       cin >> set;
      (*foo[set])(set);

    }while (s>=0 && s<2);
}

void func1(int num)
{
   cout << "This is function<< num+1;
}

void func2(int index)
{
   cout << "This is function<< index+1;
}