Ad Code

Responsive Advertisement

Function pointer as argument in C ? With example

 Function pointer as argument in C

Till now, we have seen that in C programming, we can pass the variables as an argument to a function. We cannot pass the function as an argument to another function.This allows you to create more flexible and dynamic code by passing different functions as arguments to other functions. Function pointers are commonly used in scenarios where you want to implement callback mechanisms, create plugins, or provide custom behavior for certain operations.

Therefore, C programming allows you to create a pointer pointing to the function, which can be further passed as an argument to the function. We can create a function pointer as follows:

c
return_type (*function_pointer_name)(parameter_types);


In the above syntax, the type is the variable type which is returned by the function, *pointer_name is the function pointer, and the parameter is the list of the argument passed to the function.

Let's consider an example:

c
#include <stdio.h> // Define a function pointer type for functions that take two integers and return an integer. typedef int (*BinaryOperation)(int, int); // Example functions that match the signature of the BinaryOperation function pointer type. int add(int a, int b) { return a + b; } int subtract(int a, int b) { return a - b; } int multiply(int a, int b) { return a * b; } // A function that takes a function pointer as an argument and uses it. int performOperation(int x, int y, BinaryOperation operation) { return operation(x, y); } int main() { int result; result = performOperation(5, 3, add); // Pass the 'add' function printf("5 + 3 = %d\n", result); result = performOperation(10, 4, subtract); // Pass the 'subtract' function printf("10 - 4 = %d\n", result); result = performOperation(6, 2, multiply); // Pass the 'multiply' function printf("6 * 2 = %d\n", result); return 0; }

In this example, we define a function pointer type BinaryOperation and create three functions (add, subtract, and multiply) that match its signature. The performOperation function takes two integers and a function pointer as arguments and calls the function pointed to by the function pointer with the provided arguments.

Function pointers are a powerful feature in C that allows for dynamic dispatch and customization of behavior in your programs. They are commonly used in various scenarios, such as creating libraries, plugins, and callback systems.

Let's see a simple example of how we can pass the function pointer as a parameter.


c
#include <stdio.h> // Define a function pointer type for functions that take two integers and return an integer. typedef int (*BinaryOperation)(int, int); // Example functions that match the signature of the BinaryOperation function pointer type. int add(int a, int b) { return a + b; } int subtract(int a, int b) { return a - b; } int multiply(int a, int b) { return a * b; } // A function that takes a function pointer as an argument and uses it. int performOperation(int x, int y, BinaryOperation operation) { return operation(x, y); } int main() { int result; // Pass the 'add' function pointer to performOperation result = performOperation(5, 3, add); printf("5 + 3 = %d\n", result); // Pass the 'subtract' function pointer to performOperation result = performOperation(10, 4, subtract); printf("10 - 4 = %d\n", result); // Pass the 'multiply' function pointer to performOperation result = performOperation(6, 2, multiply); printf("6 * 2 = %d\n", result); return 0; }


Output:

5 + 3 = 8 10 - 4 = 6 6 * 2 = 12

In this example, 
we pass different function pointers (add, subtract, and multiply) as arguments to the performOperation function, and it calls the appropriate function based on the provided function pointer, resulting in the expected arithmetic operations and output.

Now, we will pass the function pointer as a argument in Quicksort function "qsort". It uses an algorithm that sorts an array.

c
#include <stdio.h> #include <stdlib.h> // Comparison function for sorting integers in ascending order int compareAscending(const void *a, const void *b) { int intA = *((int*)a); int intB = *((int*)b); if (intA < intB) return -1; if (intA > intB) return 1; return 0; } // Comparison function for sorting integers in descending order int compareDescending(const void *a, const void *b) { return -compareAscending(a, b); } int main() { int arr[] = {5, 1, 8, 2, 9}; int n = sizeof(arr) / sizeof(arr[0]); printf("Original Array: "); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); // Sort the array in ascending order qsort(arr, n, sizeof(int), compareAscending); printf("Ascending Order: "); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); // Sort the array in descending order qsort(arr, n, sizeof(int), compareDescending); printf("Descending Order: "); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); return 0; }

In this example, we define two comparison functions, compareAscending and compare
  Descending, which take two pointers to integers and return an integer indicating 
the order of comparison. We then use the qsort function to sort the arr array in both
   ascending and descending order, passing the appropriate comparison function as
the function pointer.

Output :

mathematica
Original Array: 5 1 8 2 9 Ascending Order: 1 2 5 8 9 Descending Order: 9 8 5 2 1


This demonstrates how you can use function pointers to customize the behavior of the qsort function when sorting arrays.

Post a Comment

0 Comments