Function Pointer in C
Do not miss this exclusive book on Binary Tree Problems. Get it now for free.
Reading time: 25 minutes | Coding time: 4 minutes
Function pointer is a concept where a pointer variable can point to the memory address of a function. Memory address of a function means the starting of that function.This memory address is referenced by the function name itself, which means function name points to the memory address of that function.So we can access the memory address of a function by writing function name alone with out parenthesis.
Syntax of function pointer declaration
returntype (*pointername) (arguments);
Sample declaration
int (*fptr) ();
Here function pointer fptr can point to any function that takes no arguments and returns an integer.
If you want to have a function pointer to a function, then the type of function pointer must match the type of the function.That means the return type and the arguments list of function pointer must match that of the function.
You should also be careful when declaring function pointer. If you omit the parentheses around the pointer name, it will become the declaration of function that returns a pointer of specified type.
int (*fptr) ();//function pointer
int *fptr ();//function that returns an integer pointer
Lets look at the example below and understand how a function pointer is initialized and used.
#include <stdio.h>
int add(int,int);
void main()
{
// fptr is a pointer to function add()
void (*fptr)(int, int) = &add; //initialization
// Invoking fun() using fptr
(*fptr)(10,20);
}
int add(int a, int b)
{
return a+b;
}
Since function name itself pointing to the function address, we can omit '&' operator for initializing function pointer as well as we can omit '*' from function pointer while invoking a function using function pointer.See the below example.
ftr = add; //function pointer initialization
fptr(10,20);//function invocation using function pointer
Till now you have seen what a function pointer and how to declare and initialize it.Now lets see some of the usages of function pointers.
Passing function pointer as argument to a function
Like any other pointer, function pointer can also be passed as an argument to function. See the example below.
#include<stdio.h>
void add(int, int);
void multiply(int,int);
void divide(int,int);
void operation(void (*fptr)(), int, int);
int main()
{
operation(add, 2, 1);
operation(multiply, 2, 1);
operation(divide, 2, 1);
return 0;
}
void add(int a, int b)
{
printf("addition result = %d\n", a+b);
}
void multiply(int a, int b)
{
printf("multiplication result = %d\n", a*b);
}
void divide(int a, int b)
{
printf("division result = %d\n", a/b);
}
void operation(void (*fptr)(),int a, int b){
fptr(a,b);
}
Here fptr can point to any of the three (add,multiply,divide) functions,since they are of same type.Address of these functions are passed to the function operation at runtime and will be called from there.
Array of Function Pointer
Now we will see how we can have array of function pointers.
#include<stdio.h>
void add(int, int);
void multiply(int,int);
void divide(int,int);
void operation(void (*fptr)(), int, int);
int main()
{
void (*fparray[3]) (); //function poniter array declaration
fparray[0] = add;
fparray[1] = multiply;
fparray[2] = divide;
/* void (*fparray[4]) = {add, mutliply, divide}
this is another way of initializing function pointer array*/
int i=0;
for(i; i<3 ;i++)
{
operation(fparray[i], 2, 1);
}
return 0;
}
void add(int a, int b)
{
printf("addition result = %d\n", a+b);
}
void multiply(int a, int b)
{
printf("multiplication result = %d\n", a*b);
}
void divide(int a, int b)
{
printf("division result = %d\n", a/b);
}
void operation(void (*fptr)(),int a, int b){
fptr(a,b);
}
Now you must have learned how to pass function pointer as function argument as well as how to make and use array of function pointers. Similarly you can return a function pointer from a function. Lets see how that becomes possible.
#include<stdio.h>
void add(int, int);
void multiply(int,int);
void divide(int,int);
typedef void (*FPTR)(); // defining a type FPTR which is function pointer that returns void and no argument
typedef enum operations{ADD, MULTIPLY, DIVIDE
} operation;
FPTR get_operation(operation); //declaration of function get_operation which returns type FPTR which is a function pointer type
int main()
{
/* get_operation returing function pointer of type FPTR and fparry is array of type FPTR*/
FPTR fparray[3] = {get_operation(ADD), get_operation(MULTIPLY), get_operation(DIVIDE)};
int i=0;
for(i; i<3 ;i++)
{
fparray[i](1,2);
}
return 0;
}
void add(int a, int b)
{
printf("addition result = %d\n", a+b);
}
void multiply(int a, int b)
{
printf("multiplication result = %d\n", a*b);
}
void divide(int a, int b)
{
printf("division result = %d\n", a/b);
}
FPTR get_operation(operation op){
switch(op){
case ADD:
return add;
case MULTIPLY :
return multiply;
case DIVIDE :
return divide;
}
}
In order to return a function pointer from a function, you need to first create a type of function pointer using typedef. In the above program you can see how function pointer type FPTR has been created.Used this newly created type as return type of the get_operation function.
Let me conclude this article by pointing out some facts on function pointer.
- Function pointer points to executable code not to data.
- We cannot perform pointer arithmetic on function pointer.
- We cannot allocate or deallocate memory for a function pointer
Question
Identify the wrong declaration
Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.