Let us learn about the Function Pointers in C in detail.
Pointers and Functions in C
Memory addresses are stored in pointers, which are variables in C. Code segments that are reusable and carry out particular duties are called functions. In C programming, the relationship between pointers and functions is a potent feature that makes possible a number of significant features.
Pointers can interact with functions in two main ways:
Passing Pointers as Arguments (Call by Reference)
- In C, arguments are supplied by value by default, with the exception of arrays. Therefore, any changes made to the parameter inside the function do not impact the original variable in the calling function, and the function is given a duplicate of the argument’s value.
- C programs can implement pass-by-reference with pointers. A pointer to a variable is passed to the function when you supply its address as an argument (by using the & address operator). The function can then access and alter the original value kept at that address in the caller’s memory by using the indirection operator (*).
- This is especially helpful when a function needs to “return” many values or change variables in the caller, as a conventional return statement can only return one value.
- The name of an array frequently acts as a pointer to its first element, demonstrating how arrays in C are intrinsically tied to pointers. Essentially, array arguments are a type of pass-by-reference by default as when an array is supplied to a function, its base address (a pointer) is passed as well.
Example: The following code exemplifies pass-by-reference with a pointer argument:
#include <stdio.h>
void test (int *int_pointer) { *int_pointer = 100; }
int main (void) { void test (int *int_pointer); int i = 50, *p = &i;
printf ("Before the call to test i = %i\n", i);
test (p); printf ("After the call to test i = %i\n", i);
return 0; }
Output:
Before the call to test i = 50
After the call to test i = 100
In this example, myNumber’s address is passed to the modifyValue method. By using *intValuePtr = 100;, the value that is stored at that address the original myNumber variable in main is altered.
Function Pointers in C
- Function pointers hold the initial memory address of a function’s executable code, like data variable pointers.
- The compiler treats function names without parentheses as pointers.
- In order to build callback mechanisms, save functions in data structures (such as arrays of function pointers), and provide functions as parameters to other functions, function pointers are helpful.
- The syntax for declaring a function pointer can be a little complicated because it specifies the parameter and return types of the functions it can point to.
- Using the pointer variable name followed by brackets and arguments, as in a straight function call, or explicitly using the indirection operator , you can call the function that a function pointer points to. It is possible to invoke the function that fp points to using either fp() or (fp)().
Example:
#include <stdio.h>
// A simple function that adds two integers
int add(int a, int b) {
return a + b;
}
// A simple function that subtracts two integers
int subtract(int a, int b) {
return a - b;
}
int main() {
// Declare a function pointer 'operation_ptr'
// It can point to any function that takes two ints and returns an int.
int (*operation_ptr)(int, int);
// Assign the address of the 'add' function to the pointer
operation_ptr = &add; // '&' is optional but good practice for clarity
// Call the 'add' function using the function pointer
int result1 = operation_ptr(10, 5);
printf("Result of addition (via pointer): %d\n", result1);
// Assign the address of the 'subtract' function to the pointer
operation_ptr = subtract; // '&' is optional here too
// Call the 'subtract' function using the function pointer
int result2 = operation_ptr(10, 5);
printf("Result of subtraction (via pointer): %d\n", result2);
return 0;
}
Output:
Result of addition (via pointer): 15
Result of subtraction (via pointer): 5
In conclusion, function pointers are essential to working with functions in C, mainly because they support pass-by-reference semantics and because they consider functions as data that may be called and accessed indirectly through function pointers.
You can also read What Is C Pointer Arithmetic With Code Examples?