Thursday, 21 September 2017

Function pointer in C++

/*
 Demonstrating function pointer that takes two int args and return back int value
*/

#include "iostream"
using namespace std;

void printHello(){

 cout<<endl<<"Hello All"<<endl;
}

int add(int x, int y){
 return x+y; 
}
int main(){
 int (*p)(int, int);
 p = &add;
 int sum = (*p)(5,3);
 cout<<sum<<endl;

 void (*ptr)();
 ptr = printHello;
 ptr();
 return 0;
}
/*
 OUTPUT
[pavan@localhost Function-Pointer]$ g++ FPDemo.cpp
[pavan@localhost Function-Pointer]$ ./a.out 
8

Hello All
[pavan@localhost Function-Pointer]$ 
*/ 

No comments: