Function Pointer in C++

Function Pointer in C++

Function pointer in C++ is a pointer that is used to point the functions. It is generally used to store the address of a function.

A function pointer is a variable that stores the address of a function and after time they can be called through that function pointer. We can call the function also pass the pointer to another function as a parameter.

The syntax for Function Pointer

int *funcPtr(int, int)

Address of a Function

To get the address of a function, we must first say the name of the function. There is no need to call the function.

Program of the address of the function

#include <iostream>
using namespace std;
int main() {

cout << "The Address of a main() function is: " << &main << endl;
return 0;
}

Program of function pointer

of #include <iostream>  
using namespace std;  
void printname(char *name)  
{  
    std::cout << "Name is :" <<name<< std::endl;  
}  
  
int main()  
{  
    char s[20];    
    void (*ptr)(char*); 
    ptr=printname;    
    std::cout << "Enter the name : " << std::endl;  
    cin>>s;  
    cout<<s;  
    ptr(s);  
   return 0;  
}  

Output

Enter the name: 
David
David
Name is :David

 

Read more, Pointer and Array in C++

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *