Advanced C Pointer Programming chapter 4: Function pointers.

A function pointer is a pointer that holds the memory address of a function.

How to declare a function pointer in C?

Consider the function prototype as below:

void sum ();

According to the above function prototype, the function sum will not take any arguments, and will not return anything.

So we can declare a function pointer as below:

void (*fp) ();

Here “*fp” is a function pointer, that will not accept any function parameters and returns void.

The function pointer will be similar to function prototype.

More examples for function pointers:

int (*fp) (double) => “fp” is a function pointer that accepts double and returns int.

void (*fp)(char*) => “fp” is a function pointer that accepts a char pointer and returns void.

double* (*fp)(int, int) => “fp” is a function pointer that accepts two integer variables and returns a pointer to a double.

Note:
A function pointer should always be written inside a braces.

Consider a below declaration:

int *f();

The above is a simple function prototype.

Consider a below declaration:

int (*f) ();

The above is a function pointer that will not accept any variables but returns int variable.

How to use a function pointer in C?

We can make use of Pointer to function to make a call back function.

If we have a addition function as below:

int add()
{
	return (10+20);
}

For the above function, a function pointer can be declared as below:

int (*fPtr) ();

Here “*fPtr” is a function pointer, that will not take any arguments and return an integer pointer.

Now you need to assign the function to that function pointer as:

fPtr = &add;

Now you can make a function call as:

result = (*ptr)();

Full Example:

#include<stdio.h>  
int add ();  
int main ()  
{  
    int result;   
    int (*ptr)();  
    ptr = &add;  
    result = (*ptr)();  
    printf("The sum is %d\n",result);  
}  
int add()  
{   
    return (5 + 6);  
} 

Output:

The sum is 11

How to pass a function pointer ?

Passing a function pointer is very simple.

We need to create a function pointer that matches the calling function.

For Example:

#include<stdio.h>  
#include<stdlib.h> 

int add (int num1, int num2)
{
    return (num1 + num2);
}

int subtract (int num1, int num2)
{
    return (num1 - num2);
}

// fptr is a function pointer, that will accept
// 2 int values and return a int value
typedef int (*fptr) (int, int);  


// calculate is a function what accepts a function pointer
// and it will call appropriate add or subtract function
// based on the operation value.

int calculate(fptr operation, int num1, int num2)
{
    return operation(num1, num2);
}

int main ()  
{ 

    int num1 = 10;
    int num2 = 20;

    printf("The num1 = %d, num2 = %d, the addition is = %d\n", num1, num2, calculate(add, num1, num2));
    printf("The num1 = %d, num2 = %d, the subtract is = %d\n", num1, num2, calculate(subtract, num1, num2));

    return 0;
}  

Output:


The num1 = 10, num2 = 20, the addition is = 30
The num1 = 10, num2 = 20, the subtract is = -10

How to return a function pointer ?

#include<stdio.h>  
#include<stdlib.h> 

int add (int num1, int num2)
{
    return (num1 + num2);
}

int subtract (int num1, int num2)
{
    return (num1 - num2);
}

// fptr is a function pointer, that will accept
// 2 int values and return a int value
typedef int (*fptr) (int, int);  

// select is a function that will return 
// approprite function pointer
// based on the opCode
fptr select (char opCode)
{
    switch(opCode)
    {
        case '+' : return add;
        case '-' : return subtract;
    }
}

// calculate is a function what accepts a operation code as char
// and it will call appropriate add or subtract function
// based on the operation value.

int calculate(char opCode, int num1, int num2)
{
    fptr operation = select(opCode);
    return operation(num1, num2);
}

int main ()  
{ 

    int num1 = 10;
    int num2 = 20;

    printf("The num1 = %d, num2 = %d, the addition is = %d\n", num1, num2, calculate('+', num1, num2));
    printf("The num1 = %d, num2 = %d, the subtract is = %d\n", num1, num2, calculate('-', num1, num2));

    return 0;
}  

Output:

The num1 = 10, num2 = 20, the addition is = 30
The num1 = 10, num2 = 20, the subtract is = -10
Write a Comment

Leave a Comment

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