Hence, in C programming, it is possible to generate a pointer that points to a function, enabling it to be subsequently transferred as an argument to another function. The creation of a function pointer can be achieved in the following manner:
(type) (*pointer_name)(parameter);
In the provided syntax, the type represents the variable type that the function returns, *pointer_name denotes the function pointer, and the parameter indicates the collection of arguments transmitted to the function.
Example
Let's explore an illustration to showcase the usage of a function pointer as an argument in the C programming language:
float (*add)(); // this is a legal declaration for the function pointer
float *add(); // this is an illegal declaration for the function pointer
A function pointer has the ability to reference a different function, essentially storing the memory address of that 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
Explanation:
In the given scenario, a function named 'add' has been defined. Additionally, a function pointer (*a) has been defined to return a floating-point value and takes two integer parameters. Consequently, it is possible to assign the address of the add function to the 'a' pointer since they share the same float return type and integer argument types.
Now, 'a' represents a pointer directed to the add function. Invoking the add function can be achieved by utilizing the pointer 'a'. Below, we will demonstrate how to execute this operation:
a(2, 3);
The mentioned statement invokes the add function through the pointer 'a', with 'a' receiving two arguments: 2 and 3.
Another Example: Calling a Function Using Function Pointer in C
Let's explore a basic illustration demonstrating the process of passing a function pointer as an argument.
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
[Program Output]
Explanation
- 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 printnumbers function. It means that we can call the printnumbers 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 printnumbers function using statement p(i). Here, p(i) means that printnumbers function will be called on each iteration of i, and the value of 'i' gets printed.
Function pointer as argument in Quicksort
Now, we are going to provide the function pointer as an argument in the Quicksort function "qsort". This function implements a sorting algorithm for arrays.
Example
Let's consider an illustration to showcase the functionality of a function pointer as a parameter in the Quicksort algorithm implemented in the C programming language.
#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
[Program Output]
Explanation:
- 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.