We know that we can create pointers to any data type, such as int, char, and float. We can also create pointers to functions. The code of a function always resides in memory, which means that the function has a certain address. We can obtain the memory address using function pointers.
Let’s look at a simple example.
#include <stdio.h> int main() { printf("main() function address is %p", main); return 0; }
The above code prints the address of the main() function.
Output
From the above output, we can observe that the main() function has a certain address. Therefore, we can conclude that every function has an address.
Declaration of Function Pointers
So far, we have learned that functions have addresses, so we can create pointers to store these addresses and point to them.
Syntax of Function Pointers
return_type (*ptr_name)(type1, type2…);
For example:
int (*ip)(int);
In the above declaration, *ip is a pointer to a function that returns an int value and accepts integer values as parameters.
float (*fp)(float);
In the above declaration, *fp is a pointer to a function that returns a float value and accepts float values as parameters.
We can observe that the declaration of a function is similar to the declaration of a function pointer, except that there is a ‘*’ before the pointer. Therefore, in the above declaration, fp is declared as a function rather than a pointer.
👇 Click to receive 👇
👉 C Language Knowledge Resource Collection
So far, we have learned how to declare function pointers. The next step is to assign the address of the function to the function pointer.
float (*fp)(int, int); // Declaration of function pointer.
float func(int, int); // Declaration of the function.
fp = func; // Assign the address of func to the fp pointer.
In the above declaration, the ‘fp’ pointer contains the address of the ‘func’ function.
Note: The function must be declared before assigning its address to the function pointer.
Calling Functions Through Function Pointers
We have learned how to call functions in the conventional way. Now, we will see how to call functions using function pointers.
Assume we declare a function as follows:
float func(int, int); // Declaration of the function.
The conventional way to call the above function is as follows:
result = func(a, b); // Call the function in the conventional way.
Using a function pointer to call the function is as follows:
result = (*fp)(a, b); // Call the function using a function pointer.
Or
result = fp(a, b); // Call the function using a function pointer, the indirect operator can be omitted.
The effect of calling a function by its name or by its pointer is the same. If we use a function pointer, we can omit the indirect operator, as we did in the second case. However, we still use the indirect operator because it clearly indicates to the user that we are using a function pointer.
Let’s understand function pointers through an example.
#include <stdio.h> int add(int, int); int main() { int a, b; int (*ip)(int, int); int result; printf("Enter values for a and b:"); scanf("%d %d", &a, &b); ip = add; result = (*ip)(a, b); printf("The sum is: %d", result); return 0; }
int add(int a, int b) { int c = a + b; return c; }
Output
Passing Function Addresses as Parameters to Other Functions
We can pass the address of a function to other functions just like sending other parameters to a function.
Let’s understand through an example.
#include <stdio.h> void func1(void (*ptr)()); void func2(); int main() { func1(func2); return 0; } void func1(void (*ptr)()) { printf("Function 1 is called"); (*ptr)(); } void func2() { printf("\nFunction 2 is called"); }
In the above code, we created two functions, func1() and func2(). The func1() function takes a function pointer as a parameter. In the main() method, we call the func1() method, passing the address of func2. When the func1() function is called, ‘ptr’ contains the address of func2. Inside the func1() function, we call the func2() function by dereferencing the pointer ‘ptr’ because it contains the address of func2.
Output
Array of Function Pointers
Function pointers are used in applications where we do not know in advance which function will be called. In an array of function pointers, the array receives the addresses of different functions and calls the appropriate function based on the index number.
Let’s understand through an example.
#include <stdio.h> float add(float, int); float sub(float, int); float mul(float, int); float div(float, int); int main() { float x; // Variable declaration. int y; float (*fp[4])(float, int); // Declaration of an array of function pointers. fp[0] = add; // Assign the addresses of functions to the elements of the array of function pointers. fp[1] = sub; fp[2] = mul; fp[3] = div; printf("Enter values for x and y:"); scanf("%f %d", &x, &y); float r = (*fp[0])(x, y); // Call add() function. printf("\nThe sum of the two values is: %f", r); r = (*fp[1])(x, y); // Call sub() function. printf("\nThe difference of the two values is: %f", r); r = (*fp[2])(x, y); // Call mul() function. printf("\nThe product of the two values is: %f", r); r = (*fp[3])(x, y); // Call div() function. printf("\nThe division of the two values is: %f", r); return 0; } float add(float x, int y) { float a = x + y; return a; } float sub(float x, int y) { float a = x - y; return a; } float mul(float x, int y) { float a = x * y; return a; } float div(float x, int y) { float a = x / y; return a; }
In the above code, we created an array of function pointers that contains the addresses of four functions. After storing the addresses of the functions in the array of function pointers, we call the functions using the function pointers.
Output
Programmer Technical Exchange Group
Scan to join the group, remember to note: city, nickname, and technical direction.