- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to assign a pointer to function using C program?
Pointer to function
It holds the base address of function definition in memory.
Declaration
datatype (*pointername) ();
The name of the function itself specifies the base address of the function. So, initialization is done using function name.
For example,
int (*p) (); p = display; //display () is a function that is defined.
Example 1
We shall see a program for calling a function using pointer to function −
#include<stdio.h> main (){ int (*p) (); //declaring pointer to function clrscr (); p = display; *(p) (); //calling pointer to function getch (); } display (){ //called function present at pointer location printf(“Hello”); }
Output
Hello
Example 2
Let us consider another program explaining the concept of pointer to function −
#include <stdio.h> void show(int* p){ (*p)++; // add 1 to *p } int main(){ int* ptr, a = 20; ptr = &a; show(ptr); printf("%d", *ptr); // 21 return 0; }
Output
21
Advertisements