| by Arround The Web | No comments

Function Pointers in C

“Whenever we declare any variable in the program, it allocates some memory for itself, and we can access the values stored in this variable by pointing to the address of the memory location of the variable. Likewise, whenever we make a function in the program, some memory size also gets allocated for that function, and if, at any point in the program, we want to access that function, we may simply point towards the memory address of that function. This is done by the function pointers; the function pointers in the programming language C simply make the call to the function for which they are appointed as a pointer, just as a normal type of function. The declaration of the function pointer is represented with the symbol “*” and it has the same parameters of the function, just like the same function to which it points.”

Procedure

The article will cover the syntax of function pointers. It will let us know how we can cause the pointer to point to a function; in other words, how we can store the address of the function using the pointer. We will also learn with the help of this article the declaration and reference of the function pointer and how the function pointers can be used to make the function calls for the pointer function.

Syntax

The general syntax for the declaration of the function pointer is as follows:

$ Return type of function (* pointer name ) (data type ar_1, data type arg_2)

The declaration method of the function pointer is as same as the usual function declaration; the only difference in the function pointer is when we declare the name of the function pointer, we use the symbol “*” unlike the normal function where the name is declared without the use of any symbol with it. Let’s declare a dummy function pointer with the help of some hypothetical example; first, declare any function with the usual method, e.g., “int func (int, int).”

After the declaration of function “func,” we will now declare a function pointer as “int *func_pointer (int, int).” The “*func_pointer” with return type as an integer is now the function pointer for the function “func,” and it has the information regarding the memory address of the function “func.”

Example # 01

After the above-mentioned explanation of the declaration of the function pointer, we are now familiar with the declaration of the function pointer. So, we will use this declaration method to create a function pointer and then make use of this function pointer to make the function calls that will return the function that we would have already declared. For practically implementing this scenario, we will use the Visual compiler studio for C. We will create a project and add it to the path of “C” directories by saving the project with “.c” as an extension.

After creating the project, we will allow the program to also use the functions from the “stdio.h” header file. We will now declare a normal function with the name “square” and the return type as the integer with the two function parameters, both with the data type as an integer. Then we will declare the main function with a return type integer, and in the main function, we will initialize the two variables as “l” and “w.” With the initialization of these two variables, we will declare the function pointer for the already declared function “square,” and we will do this by setting the return type of function pointer as integer and its name as “*sp” with function parameters integers as “int *sp (int, int).”

To this function pointer sp, we will assign the square function and will read the value of the already initialized variables l and w by calling the scanf () by passing the address of l and w to the arguments of the scanf() method as “&l” and “&w” respectively. Now we will call the function with the pointer function and multiply the value of the pointer function with the already read values of l and w and then print this value using the printf() function and exit the main function by returning zero. We will define the square function by taking l and w as the function parameters.

In the body of the function, we will simply take the multiplication of both the variables l and w to compute the “area” of the square and exit the function returning this area. The implementation of the examples is shown in the following figure.

#include<stdio.h>

int square(int, int);

int main() {

    int l,w, area;
    int (*sp)(int, int);

    printf("Enter length and breadth of a rectangle\n");
    scanf("%d%d", &l, &w);
    sp=square;
    area = (*sp)(l,w);
    printf("Area of rectangle = %d", area);
    return 0;
}
int square(int l, int w) {
    int area1= l * w;
    return area1;

}

We have given the same user input for both the variables l and w as “5” since they both represent the length and width of the square, which are equal in value. The output has returned the square of value 5 as the area of the square.

Example # 02

With the help of this example, we will try to declare the function pointer for the arrays. We will create an array of the function pointers where every element of the array will be storing the pointer function for different functions. For example, we will declare the two normal functions of subtraction and addition with the return type integer with two function parameters having the integer data type. Then we will declare an array function pointer and then initialize the two variables having data type “int” and will read their values using the scanf() method. Then we will use the array of pointer function with the index of the array function pointer, e.g., “arr[i]”.

For each index of this function pointer array, we will call the addition and subtraction functions. This addition and subtraction will use the read values of initialized variables “a” and “b”. We will then display these results by calling the printf() method. It should remain clear that we had to define the addition and subtraction function where we will add and subtract the variable “a” and “b” in the body of these functions and will return the results to these functions. The following figure shows the complete program for this example:

#include<stdio.h>

int addition(int, int);

int subtraction (int, int);

int main() {

    int a, b;
    float (*function [2]) (int, int);
    function[0] = addition;
    function [1] = subtraction;
    printf ("Enter two values ");
    scanf ("%d %d", &a, &b);
    float result = (*function [0]) (a, b);
    printf ("Addition (a+b) = %.1f\n", result);
    result = (*function [1]) (a, b);
    printf ("Subtraction (a-b) = %.1f\n", result);
    return 0;}
int addition (int a, int b) {
    return a + b;}
int subtraction (int a, int b) {
    return a - b;

}

Conclusion

The guide contains all the necessary information related to function pointers in the C language. Both the syntax for the declaration and the examples have been shown for the implementation of function pointers in this guide. We hope that this guide will help you to understand the concept of function pointers in the best possible way.

Share Button

Source: linuxhint.com

Leave a Reply