Demonstrations of Function Pointers


/***************************************************************************
This program demonstrates how to declare various types of function pointers:

 simple function pointers,
 array of function pointers,
 pointers to function pointers,
 array of pointers to function pointers.

 12/10/98, Dr. Yingcai Xiao
*****************************************************************************/

#include <iostream.h>

int g = 5;   // declaring a global variable
int fri() {return 0;}  // defining a function that returns an int.
int * frip() {return &g;} // defining a function that returns an int pointer.

void main()
{
 cout << "/* Demonstrations of Function Pointers */" << endl << endl;
 cout << "A global variable: " << endl;
 cout << "int g = 5; \n\n";

 cout << "A function returning an int :" << endl;
 cout << "int fri() {return 0;} \ncout << fri(); => "
   << fri() << endl << endl;

 cout << "A function returning an int pointer :" << endl;
 cout << "int * frip() {return &g;} \ncout << frip(); => "
   << frip() << endl << endl;

 cout << "A function prototype :" << endl;
 cout << "int *f1(); // f1 returns an int pointer." << endl << endl;
 int *f1();  // function prototype, f1 returns an int pointer.
 //f1 = fri;      // error, lvalue needed
 //f1 = frip;     // error, lvalue needed

 cout << "Another function prototype :" << endl;
 cout << "int *(f2()); // f2 returns an int pointer." << endl << endl;
 int *(f2());  // function prototype, f2 returns an int pointer.
 //f2 = fri;      // error, lvalue needed
 //f2 = frip;     // error, lvalue needed

 cout << "A function pointer to a function returning an int : \n";
 int (*f3)();   // function pointer to a function returning an int.
 f3 = fri;
 cout << "int (*f3)(); \nf3 = fri; \ncout << f3(); => "
   << f3() << endl << endl;

 cout<<"A function pointer to a function returning an int pointer :\n";
 int *(*f4)();  //function pointer to a function returning an int pointer.
 f4 = frip;
 cout << "int *(*f4)(); \nf4 = frip; \ncout << f4(); =>"
  << f4() << endl;
 cout << "cout << *f4(); => " << *f4() << endl << endl;
 cout << "cout << (*f4)(); => " << (*f4)() << endl << endl;

 cout<<"An array of function pointers to functions returning ints :\n";
 int (*f5[2])() = {fri,fri};     // array of function pointers to
             // functions returning ints.
 cout << "int (*f5[2])() = {fri,fri};" << endl;
 cout << "f5[0](); => " << f5[0]() << endl;
 cout << "f5[1](); => " << f5[1]() << endl << endl;

 cout<<"An array of function pointers to functions returning int pointers :\n";
 int *(*f6[2])() = {frip,frip};  // array of function pointers to
             // functions returning int pointers.
 cout << "int *(*f6[2])() = {frip,frip}; \n";
 cout << "f6[0](); => " << f6[0]() << endl;
 cout << "f6[1](); => " << f6[1]() << endl << endl;

 cout<<"A pointer to a function pointer to a function returning an int :\n";
 int (*(*f7))();   // pointer to a function pointer
       // to a function returning an int.
 f7 = &f3;
 cout << "int (*(*f7))(); \nf7 = &f3; \n";
 cout << "(*f7)(); => " << (*f7)() << endl << endl;

 cout<<"A pointer to a function pointer to a function returning an int :\n";
 int (**f8)();     // pointer to a function pointer
       // to a function returning an int.
 f8 = &f3;
 cout << "int (**f8)(); \nf8 = &f3; \n";
 cout << "(*f8)(); => " << (*f8)() << endl << endl;

 cout<<"An array of pointers to function pointers to functions returning ints :\n";
 int (*(*f9[2]))() = {&f3, &f3}; // array of pointers to function pointers
            // to functions returning ints.
 cout << "int (*(*f9[2]))() = {&f3, &f3}; \n";
 cout << "(*f9[0])(); => " << (*f9[0])() << endl;
 cout << "(*f9[1])(); => " << (*f9[1])() << endl << endl;

 cout<<"An array of pointers to function pointers to functions returning ints :\n";
 int (**f10[2])() = {&f3, &f3}; // an array of pointers to function pointers
            // to functions returning ints.
 cout << "int (**f10[2])() = {&f3, &f3}; \n";
 cout << "(*f10[0])(); => " << (*f10[0])() << endl;
 cout << "(*f10[1])(); => " << (*f10[1])() << endl << endl;

 cout<<"A pointer to a function pointer to a function returning an int pointer :\n";
 int *(*(*f11))();    // pointer to a function pointer
        // to a function returning an int pointer.
 f11 = &f4;
 cout << "int *(*(*f11))(); \nf11 = &f4; \n";
 cout << "(*f11)(); => " << (*f11)() << endl << endl;

 cout<<"A pointer to a function pointer to a function returning an int pointer :\n";
 int *(**f12)();      // pointer to a function pointer
        // to a function returning an int pointer.
 f12 = &f4;
 cout << "int *(**f12)(); \nf12 = &f4; \n";
 cout << "(*f12)(); => " << (*f12)() << endl << endl;

 cout<<"An array of pointers to function pointers to functions returning int pointers :\n";
 int *(*(*f14[2]))() = {&f4, &f4}; // array of pointers to function pointers
             // to functions returning int pointers.
 cout << "int *(*(*f14[2]))() = {&f4, &f4}; \n";
 cout << "(*f14[0])(); => " << (*f14[0])() << endl;
 cout << "(*f14[1])(); => " << (*f14[1])() << endl << endl;

 cout<<"An array of pointers to function pointers to functions returning int pointers :";
 cout << endl;
 int *(**f15[2])() = {&f4, &f4};  // array of pointers to function pointers
             // to functions returning int pointers.
 cout << "int *(**f15[2])() = {&f4, &f4}; \n";
 cout << "(*f15[0])(); => " << (*f15[0])() << endl;
 cout << "(*f15[1])(); => " << (*f15[1])() << endl << endl;

 cout << "That's all, folks!" << endl;

}
 


/* Demonstrations of Function Pointers */

A global variable:
int g = 5;

A function returning an int :
int fri() {return 0;}
cout << fri(); => 0

A function returning an int pointer :
int * frip() {return &g;}
cout << frip(); => 0x436f0076

A function prototype :
int *f1();      // f1 returns an int pointer.
Another function prototype :
int *(f2());    // f2 returns an int pointer.

A function pointer to a function returning an int :
int (*f3)();
f3 = fri;
cout << f3(); => 0

A function pointer to a function returning an int pointer :
int *(*f4)();
f4 = frip;
cout << f4(); =>0x436f0076
cout << *f4(); => 5
cout << (*f4)(); => 0x436f0076

An array of function pointers to functions returning ints :
int (*f5[2])() = {fri,fri};
f5[0](); => 0
f5[1](); => 0

An array of function pointers to functions returning int pointers :
int *(*f6[2])() = {frip,frip};
f6[0](); => 0x436f0076
f6[1](); => 0x436f0076

A pointer to a function pointer to a function returning an int :
int (*(*f7))();
f7 = &f3;
(*f7)(); => 0

A pointer to a function pointer to a function returning an int :
int (**f8)();
f8 = &f3;
(*f8)(); => 0

An array of pointers to function pointers to functions returning ints :
int (*(*f9[2]))() = {&f3, &f3};
(*f9[0])(); => 0
(*f9[1])(); => 0

An array of pointers to function pointers to functions returning ints :
int (**f10[2])() = {&f3, &f3};
(*f10[0])(); => 0
(*f10[1])(); => 0

A pointer to a function pointer to a function returning an int pointer :
int *(*(*f11))();
f11 = &f4;
(*f11)(); => 0x436f0076

A pointer to a function pointer to a function returning an int pointer :
int *(**f12)();
f12 = &f4;
(*f12)(); => 0x436f0076

An array of pointers to function pointers to functions returning int pointers :
int *(*(*f14[2]))() = {&f4, &f4};
(*f14[0])(); => 0x436f0076
(*f14[1])(); => 0x436f0076

An array of pointers to function pointers to functions returning int pointers :
int *(**f15[2])() = {&f4, &f4};
(*f15[0])(); => 0x436f0076
(*f15[1])(); => 0x436f0076

/* That's all, folks! */