In Function Pointer As Argument In C Programming

Share Your Love

We’ve seen how to pass variables as an argument to a function in C programming so far. The function cannot be passed as an argument to another function. A function pointer, on the other hand, can be used to pass the reference to a function as a parameter. Because the function parameter is supplied as a pointer that holds the address of arguments, this is known as call by reference. If the function uses pointers, any changes made by the function will be reflected in the address of the given variable.

As a result, in C programming, you can build a pointer to a function, which can then be supplied as an argument to the function. We can make a function pointer in the following way:

(type) (*pointer_name)(parameter);  

The “type” is the variable type returned by the function, *pointer_name is the function pointer, and the parameter is the list of arguments supplied to the function in the above syntax.

Let’s consider an example:

// this is a legal declaration for the function pointer
float (*add)();
// this is an illegal declaration for the function pointer
float *add(); 

A function pointer can also be used to point to another function or to contain the address of another function.

float add (int a, int b);  // function declaration
float (*a)(int, int);  // declaration of a pointer to a function
a=add; // assigning address of add() to 'a' pointer  

We’ve declared a function called ‘add’ in the example above. We’ve also declared the function pointer (*a), which returns a floating-point number and has two integer-type parameters. Because both the return type (float) and the type of arguments are the same, we can now assign the address of the add() method to the ‘a’ pointer.

‘a’ now serves as a pointer to the add() method. We can use the pointer, ‘a,’ to call the add() function. Let’s have a look at how we can accomplish this:

a(2, 3);  

The above sentence uses the pointer ‘a’ to call the add() function, and two parameters, 2 and 3, are provided to ‘a’.

Let’s look at a simple example of how the function pointer can be passed as a parameter:

void display(void (*p)())
{
    for(int i=1;i<=5;i++)
    {
        p(i);
    }
}
void print_numbers(int num)
{
    cout<<num;
}
int main()
{
    void (*p)(int);     // void function pointer declaration
    printf("The values are :");
  	display(print_numbers);
    return 0;
}  

Output:

The values are: 1
2
3
4
5

In the above code:

  • We have defined two functions named ‘display()’ and print_numbers().
  • Inside the main() method, we have declared a function pointer named as (*p), and we call the display() function in which we pass the print_numbers() function.
  • When the control goes to the display() function, then pointer *p contains the address of print_numbers() function. It means that we can call the print_numbers() function using function pointer *p.
  • In the definition of display() function, we have defined a ‘for’ loop, and inside the for loop, we call the print_numbers() function using statement p(i). Here, p(i) means that print_numbers() function will be called on each iteration of i, and the value of ‘i’ gets printed.

Now, we will pass the function pointer as an argument in Quicksort function “qsort()”. It uses an algorithm that sorts an array.

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int compare(const int *p, const int *q);
int (*f)(const void *a, const void *b);
int main()
{
    int a[]={4,7,6,1,3,2};
    int num=sizeof(a)/sizeof(int);
    f=&compare;
    qsort(a, num, sizeof(int), (*f));
    for(int i=0;i<num;i++)
    {
        printf("%d ,",a[i]);
    }
}
int compare(const int *p, const int *q)
{
     if (*p == *q)
        return 0;
    else if (*p < *q)
        return -1;
    else
        return 1;
}

Output:

1, 2, 3, 4, 6, 7

In the above code:

  • We have defined an array of integer type. After creating an array, we have calculated the size of an array by using the sizeof() operator, and stores the size in the num
  • We define a compare() function, which compares all the elements in an array and arranges them in ascending order.
  • We also have declared the function pointer, i.e., (*f), and stores the address of compare() function in (*f) by using the statement f=&compare.
  • We call qsort() function in which we pass the array, size of the array, size of the element, and the comparison function. The comparison function, i.e., compare() will compare the array elements until the elements in an array get sorted in ascending order.

Share Your Love
Avatar photo
Lingaraj Senapati

Hey There! I am Lingaraj Senapati, the Founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.

Articles: 355

Newsletter Updates

Enter your email address below to subscribe to our newsletter