Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 declare a pointer to a function in C?
In C, a pointer is a variable whose value is the address of another variable or memory block, i.e direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable or block address.
Basic Pointer to a Function
A pointer to a function is simply a variable that stores the address of a function instead of a normal data value.
Syntax
Following is the syntax of basic pointer to a function:
Datatype *variable_name
Algorithm
Following is the algorithm for the basic pointer to a Function:
Begin.
Define a function show.
Declare a variable x of the integer datatype.
Print the value of varisble x.
Declare a pointer p of the integer datatype.
Define p as the pointer to the address of show() function.
Initialize value to p pointer.
End.
Example
This is a simple example in C to understand how the concept of a pointer to a function works. The program creates a function pointer p to store the address of show(), then after it calls indirectly to print the value as the output.
#include<stdio.h>
void show(int x)
{
printf("Value of x is %d\n", x);
}
int main()
{
// Assign function address
void (*p)(int);
// declaring a pointer
p = &show;
// p is the pointer to the show()
(*p)(7);
//initializing values.
return 0;
}
Following is the output to the above program:
Value of x is 7.
Function Pointer with Return Value
A function pointer with a return value is used to store the address of a function that takes input, executes, and returns a result dynamically.
Syntax
Following is the syntax is as follows:
return_type (*pointer_name)(parameter_list); // Declaration pointer_name = function_name; // Assignment
Algorithm
Following is the algorithm for the function pointer with Return Value:
Begin.
Define a function add that takes two integer parameters.
Inside add, return the sum of the two integers.
Declare a function pointer named func_ptr that matches the signature of add.
Assign the address of the add function to func_ptr.
Call the function through func_ptr with two integer arguments.
Store the return value in a variable called result.
Print the value of result.
End.
Example
In this example, we define a function add() that stores its address in a function pointer (func_ptr) by calling it indirectly with the values and prints the result.
#include<stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
// Declare function pointer
int (*func_ptr)(int, int);
// Assign function address
func_ptr = add;
int result = func_ptr(3, 4);
// Call function through pointer
printf("Result = %d\n", result);
return 0;
}
The above program produces the following result:
Result = 7