An example use of a function pointer source code

This snippet submitted by Jayesh on 2006-07-13. It has been viewed 12455 times.
Rating of 5.7 with 107 votes

 #include<iostream>
using namespace std;
/* typedef a function pointer for a function returning int and taking argument as int.
*/
typedef int(*pt2Func)(int);

/* function performing square */
int sqr(int x)
{
 return(x*x);
}
/* function returning function pointer pt2Func */
pt2Func GetPtr(const char Code)
{
 return (*sqr);
}   

int main()
{
    pt2Func xyz = GetPtr('j');
    cout << (*xyz)(5) << endl;
    getch();
    return 0;
}



 




More C and C++ source code snippets