Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests - Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests -

Function Pointer in C

One can create pointers in the C language of any data type, like char, int, float, etc. Similarly, it is easy to create pointers that point to various functions in the C language.

In this article, we will take a look at the function pointer in C and its uses according to the GATE Syllabus for CSE (Computer Science Engineering). Read ahead to learn more.

Table of Contents

What is a Function Pointer in C?

The use of pointers as function parameters is to hold (point at) the addresses of various arguments that are passed during the function calls. It is termed a call by reference. Thus, whenever a function gets called by reference in a program, any changes that we make on the reference variable would have a direct impact on its original value.

A function’s code resides in the memory. It means that every function has an address of its own. Also, we can use the function pointer to get the exact address of this memory.

Example for Function Pointer in C

Let us look at an example for the same.

#include <stdio.h>

int main()

{

printf(“The address of the main() function in this program is %p as in function pointers”,main);

return 0;

}

The code mentioned here above prints the main() function’s address in the program.

The output of the code mentioned above would be:

The address of the main() function in this program is 0x400536 as in function pointers

In the output mentioned above, we can observe that this main() function consists of an address of its own. Thus, it can be concluded that each function consists of a unique address of its own.

The Process of Declaration of the Function Pointers

Now that you are aware that every function consists of an address, one can create various pointers consisting of these addresses (storing the addresses), and thus, it can point at them in a program.

Syntax

return type (*pointer_name)(type_A, type_B… type_Z);

Let us take a look at an example to understand this better:

int (*ip) (int);

The declaration mentioned above has a pointer *ip pointing to a function. It returns a certain int (integer) value and also accepts an integer in the form of an argument. Now let us take a look at another example.

float (*fp) (float);

The declaration mentioned above consists of a pointer *fp pointing towards a certain function. It returns some float (floating) value as it accepts the argument in the form of a float value.

Observe that the declaration of any given function in a program is very similar to how we declare its function pointer. the only difference here is that the pointers to functions are preceded by the symbol ‘*’. Thus, in the case mentioned above, the declaration of fp occurs in the form of a function, and not in the form of a pointer.

Now that we have uncovered how one can declare a function pointer, the next step would be to assign these function pointers with separate addresses.

float (*ip) (int , int); // Declaring the function pointer in the program

float funct ( int , int ); // Declaring the function in the program

ip = funct; // We assign the address of the funct to the pointer ip

The pointer ‘ip’ in the declaration mentioned above consists of the ‘funct’ function’s address in the program.

Note that the declaration of functions is a prerequisite before you assign the function pointer with the available function’s address.

Calling of a Function with the Function Pointer in C

Till now, we have discussed how we can call any function in a general way. Next, we will discuss how we can use the function pointer to call any function in a program.

Let us suppose that we declare any given function as follows:

float funct(int , int); // Declaring a function in the program

Here is how we can call the above function in a very usual way:

result = funct( x , y ); // We call the function using the usual way

Here is how we can call a function with the use of the function pointer:

result = (*fp)( x , y ); // We call the function with the use of function pointers

Or

result = fp( x , y ); // We call the function with the use of function pointer, and we can remove the indirection operator

When we call a function by its own name, it has the same effect as calling it using its function pointer. When using the function pointers, one can take a step ahead and omit the indirection operator, just like we have portrayed in the second case. Yet, we can utilise the indirection operator since it makes it very clear to all the users that they can use a function pointer.

We will take a look at an example to better understand function pointers in C:

#include <stdio.h>

int sub(int,int); // Mentioning the subtraction of values

int main()

{

int x,y;

int (*fp)(int,int); // Generating the output

int output;

printf(“Here, we enter the actual values of the variables x and y : “);

scanf(“%d %d”,&x,&y);

ip=sub;

result=(*fp)(x,y);

printf(“The total value of the final variable after subtracting one variable from the other one is equal to : %d”,output);

return 0;

}

int sub(int x,int y)

{

int z=x-y;

return z;

}

The output obtained from the code mentioned above would be:

Here, we enter the actual values of the variables x and y : 38 26

The total value of the final variable after subtracting one variable from the other one is equal to : 12

The Passing of the Function Address to Another Function as an Argument

One can pass the address of a function to another function in the form of an argument the exact way we send an argument to a function in a program.

Let us take a look at an example to understand this better.

include <stdio.h>

void func_A (void (*pntr)());

void func_B ();

int main()

{

func_A (func_B);

return 0;

}

void func_A (void (*pntr)())

{

printf(“The function A is known as”);

(*pntr)();

}

void func_B ()

{

printf(“\nThe function B is known as”);

}

The output obtained from the code mentioned above would be as follows:

The function A is known as

The function B is known as

In the code mentioned above, we have created two separate functions- the func_A and the func_B. Here, the func_A consists of the function pointer in the form of an argument. This func_A method gets called in the main() method- in which we will be passing the func_B address. The ptr consists of this address of func_B when we call the func_A. Thus, we call the function func_B inside the function func_A by dereferencing the ptr pointer, since it consists of the func_B function’s actual address.

Function Pointer Arrays

We use the function pointers in those applications where we don’t know which of the functions will be called (we don’t know in advance). In the case of an array of various function pointers, the array would take the address of all the functions. After this, the calling of the most appropriate function will occur on the basis of the index number.

We will take a look at this with the help of a simple example. Here is how:

#include <stdio.h>

float div (int,float);

float mul (int,float);

float sub (int,float);

float add (int,float);

int main()

{

float a; // declaration of a variable

int b;

float (*ip[4]) (int,float); // declaration of the function pointer

ip[0]= div; // we assign addresses to all the elements of the array of all function pointers

ip[1]= mul;

ip[2]= sub;

ip[3]= add;

printf(“Please enter the respective values of the variables a and b : “);

scanf(“%f %d”,&a,&b);

float j=(*ip[0]) (a,b); // We are calling the div() function in the program

printf(“\nThe division of the two values will generate the result : %f”,j);

j=(*ip[1]) (a,b); // We are calling the mul() function in the program

printf(“\nThe multiplication of the two values will generate the result : %f”,j);

j=(*ip[2]) (a,b); // We are calling the sub() function in the program

printf(“\nThe difference between the two values will generate the result : %f”,j);

j=(*ip[3]) (a,b); // We are calling the add() function in the program

printf(“\nThe addition of the two values will generate the result : %f”,j);

return 0;

}

float div(float a,int b)

{

float t=a/b;

return t;

}

float mul(float a,int b)

{

float t=a*y;

return t;

}

float sub(float a,int b)

{

float t=a-b;

return t;

}

float add(float a,int b)

{

float t=a+b;

return t;

}

In the code mentioned above, we have actually created an array of various function pointers consisting of the addresses of all the four functions. Once we store the addresses of all these functions in the function pointers’ array, we then use the function pointers to call their respective functions.

The output obtained out of the code mentioned above would be:

Please enter the respective values of the variables a and b : 5 4

The division of the two values will generate the result : 1.250000

The multiplication of the two values will generate the result : 20.000000

The difference between the two values will generate the result : 1.000000

The addition of the two values will generate the result : 9.000000

Functions that Return Pointer Variables

The functions in a program are also capable of returning a pointer that points towards the calling function. But be very careful in this case. It is because the function’s local variables don’t reside out of it. They rather have their scope inside the functions themselves. Thus, if we return the pointers connected to the local variables, that particular pointer would point towards nothing at all when this function would end.

#include <stdio.h>

int* larger(int*, int*);

void main()

{

int p = 15;

int q = 92;

int *x;

x = larger(&p, &q);

printf(“The number %d is larger in value”,*x);

}

int* larger(int *a, int *b)

{

if(*a > *b)

return a;

else

return b;

}

The output obtained out of the function mentioned above would be:

The number 92 is larger in value

Safe Ways in Which We Can Return a Pointer that is Valid

  1. We use the arguments along with the functions. We can do so because the declaration of all the arguments that pass on to the functions occurs inside the calling functions. Thus, these will also reside outside the given function.
  2. Conversely, we can also make use of the static local variables inside the available functions and then return them. Here, since the static variables have a complete lifetime in which the main() function would exist, these will be thus available throughout any program.

Examples of Complicated Types of Function Pointers

We will understand the concept of complicated function pointers with the help of an example here. Let us see how:

void *(*foo) (int*);

Now, this one would appear complex- but it is actually very simple in reality. Here, in this case, the (*foo) is acting as a pointer towards that function in which the argument is of the type int* while the return type here is void*.

Practice Problems on Function Pointer in C

1. What would be the output obtained out of the program mentioned below?

#include <stdio.h>

int sum(int a, int b)

{

return a+b;

}

int main( )

{

int (*fp)(int, int);

fp = total;

int t = fp(10, 15);

printf(“The output of both the numbers is equal to %d”, t);

return 0;

}

A. The output of both the numbers is equal to 15

B. The output of both the numbers is equal to 10

C. The output of both the numbers is equal to 25

D. The output of both the numbers is equal to 5

Answer: C. The output of both the numbers is equal to 25

2. What would be the output obtained out of the program mentioned below?

#include <stdio.h>

int* larger(int*, int*);

void main()

{

int g = 44;

int h = 87;

int *n;

n = smaller(&g, &h);

printf(“The number %d is comparatively smaller in value”,*n);

}

int* larger(int *r, int *s)

{

if(*r > *s)

return r;

else

return s;

}

A. The number is comparatively smaller in value

B. The number 44 is comparatively smaller in value

C. 44 The number is comparatively smaller in value

D. The number 44 is comparatively smaller in value

Answer: D. The number 44 is comparatively smaller in value


Frequently Asked Questions

Q1

When do we use an array of function pointers in a C program?

We use the function pointers in those applications in which we don’t know which of the functions will be called (we don’t know in advance). In the case of an array of various function pointers, the array would take the address of all the functions. After this, the calling of the most appropriate function will occur on the basis of the index number.

Q2

Why do we use pointers in the C language?

The use of pointers as function parameters is to hold (point at) the addresses of various arguments that are passed during the function calls. It is termed a call by reference. Thus, whenever a function gets called by reference in a program, then any changes that we make on the reference variable would have a direct impact on its original value.
A function’s code resides in the memory. It means that every function has an address of its own. Also, we can use the function pointer to get the exact address of this memory.

Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility Criteria, GATE 2023, GATE Admit Card, GATE Syllabus for CSE (Computer Science Engineering), GATE CSE Notes, GATE CSE Question Paper, and more.

Also Explore,

Comments

Leave a Comment

Your Mobile number and Email id will not be published.

*

*