A Detail Guide of Function Pointer in C with programming example

Function Pointer in C


In the C programming language, a function pointer is an important pointer tool. Most of the time, the function pointer is misunderstood and ignored by the people. Because of the inappropriate explanation of declaration, and dereferencing of a function pointer.

Misconception on the function pointer creates various hidden issues in your project, which takes a lot of time in resolving.

Function Pointer in C is a very important and difficult topic to understand. Very few people understand and utilize this concept.

See Also: Types of Pointers in C

What is Function Pointer in C?

Function Pointer is similar to the other pointers in C. The difference between C function pointer and in other pointers is that function pointer points to a function instead of the variable.

Source: mycodeschool 

In other words, the function pointer points to the address of a function and these function used in the program whenever needed.

Declaration of Function Pointer in C

Declaration of a function pointer is very easy. It looks complicated, but once you start learning it becomes very easy for you.

See Also: Dangling Pointers in C

Declaration of a function pointer is similar to the C functions. So, it needs parameter list, declaration name, and return type. You should always remember at the time of declaration the name of the function pointer is preceded by the '*' (asterisk) symbol and encloses in parenthesis.

Syntax

return type (*Name_of_Function_Pointer) (argument_list);

For instance,
int (*add) (int*, int*);

In the above example, 'add' is a name of function pointer which takes two parameters (int*, int*) and returns int.

See Also: Double Pointer

In the declaration of function pointers in C, brackets play a significant role. In the above example, if brackets are eliminated, then the meaning of the above statement will completely change, and we get int *add (int*, int*). It is a declaration of function which takes (int*, int*) as parameters and returns int.

Initialization of Function Pointer in C

Working of a function pointer is similar to the pointers. We need to define function pointers after the declaration of it.

A function pointer is initialized to the address of a function, but the signature of the function pointer should be the same as the function. For instance, a function pointer taking two parameters and returns an integer value. This pointer pointing to the address of a function 'SUB' is used in the subtraction of two numbers.

#include <stdio.h>
#include <stdlib.h>
int SUB (int a, int b) //Function definition
{
int c;
c = a - b;
return c;
}

We define a function which prints the output of subtraction of two numbers when the function SUB is called.

int main()
{
int q = 0;
int (*p) (intint) = NULL; //Declaration of function pointer
p = SUB; //Initialization of Function pointer
q = (*p) (50, 20); //Calling the function pointer
printf ("\n\nSubtraction of two number = %d\n\n", q);
return 0;
}

Output
Subtraction of two number = 30

Some Essential Points for Pointer to function

We are discussing some important points for function pointer in C.

A) Just like pointers, function pointers cannot allocate de-allocate memory. It is used to point the address of the function. So, if we allocate memory, then there is no need for a function pointer.

int (*ptr) (int,int) = malloc(sizeof(ptr)); // No use

B) A function name is used to get the address of the function. The function name is sufficient to get the address of the function, no need to use '&' (ampersand operator) to get the address of the function.

ptr = SUB;
OR
ptr = &SUB;

C) You can call the function with the help of function pointer after initialization. Let's understand the function calling,

1) Name of the function pointer, ptr
2) Pointer to function is like normal pointers, but the only difference between function pointer and the normal pointer is that function pointer pointing to the function instead of variable. That's why we use '*' operator before the name of the function pointer to take the value of the function pointer. *ptr
3) Call the function with parameters, (*ptr)(50,60);

C Program helps you to understand the functioning of the pointer to function.
#include <stdio.h>
#include <stdlib.h>
//Function definition to subtract two numbers.
int SUB (int a, int b);
{
return (a-b);
}
int main()
{
int c;
int (*ptr)(int,int) = NULL; //Declaration of function pointer "ptr"
ptr = SUB; //Initialization of function pointer (Assign the address of function "SUB" to the Function pointer "ptr")
c = (*ptr) (60,50); //Function Calling
printf ("Subtraction of two numbers is %d\n", 10);
return 0;
}

Output
Subtraction of two numbers is 10

D) You can use the function pointer in C with the comparison operator (!= or ==). These operators help you to find out function pointer is initialized or not, and it considered as a good practice.
If the valid address is not assigned to the function pointer and application wants to execute the function pointer then you will get Blue screen of death (BSOD), or system hangs issues. So, always check the validity of the function pointer. For instance,

if (add==NULL)
{
return invalid;
}
else
{
(*add) (state);//Calling function
}

E) In the C programming language, function pointer can be used as an argument into the function. Let's understand with the help of C program how to function pointer is used as the argument into the function.

#include <stdio.h>
typedef int (*fpatr)(int,int); //Function Pointer
//Function Pointer as arguments
int mul_operation (int value1, int value2, fpatr cal)
{
int ret_value = 0;
ret_value = cal (value1, value2);
return ret_value;
}
//Function for multiplication of two numbers
int mul (int value1, int value2)
{
int value3;
value3 = value1*value2;
return value3;
}
int main()
{
int value1=0;
int value2=0;
int out=0;
printf ("Enter the data");
scanf ("%d%d", &value1, &value2);
out = mul_operation (value1,value2,mul);
printf ("Multiplication of two numbers is %d\n",out);
return 0;
}

Output
Enter the data 10 20
Multiplication of two numbers is 200

F) Learn how a function can return a function pointer in C with the help of program example.

#include <stdio.h>
//type declaration of function pointer
typedef int (*pfatr)(int, int);
//function for the addition of two number*/
int Add (int value1 ,int value2)
{
int value3;
value3 = value1+value2;
return (value3);
}
//function for the subtraction of two number
int Sub (int value1 ,int value2)
{
return (value1 - value2);
}
//function for the multiplication of two number
int Mul (int value1 ,int value2)
{
return (value1 * value2);
}
//Function for the division of two number
int Div (int value1, int value2)
{
int value3;
value3 = value1/value2;
return value3;
}
//Return function pointer
pfatr Operations (int Choice)
{
//function pointer
pfatr Function = NULL;
switch (Choice)
{
case 1:
Function = Add;
break;
case 2:
Function = Sub;
break;
case 3:
Function = Mul;
break;
case 4:
Function = Div;
break;
}
return Function;
}
int main (void)
{
int value1 = 0;
int value2 = 0;
int Choice = 0;
int Result = 0;
pfatr Function = NULL; //function pointer
printf ("Enter Data \n\n");
scanf ("%d%d",&value1,&value2);
printf ("Enter 1 for the Addition \n\n");
printf ("Enter 2 for the Subtraction \n\n");
printf ("Enter 3 for the Multiplication \n\n");
printf ("Enter 4 for the division \n\n");
scanf ("%d",&Choice);
Function = Operations (Choice);
//verify the pointers
if (Function != NULL)
{
Result = (*Function) (value1,value2);
printf ("Result is %d\n\n",Result);
}
else
{
printf ("Enter the choice\n");
}
return 0;
}

Output
Function Pointer in C

Use of Array of Function Pointer in C

The C array of function pointers is similar to the pointers. You can access the array of function pointer using an array index. For instance,

#include <stdio.h>
#include <stdlib.h>
int Add (int value1 ,int value2)
{
int value3;
value3 = value1+value2;
return (value3);
}
//function for the subtraction of two number
int Sub (int value1 ,int value2)
{
return (value1 - value2);
}
//function for the multiplication of two number
int Mul (int value1 ,int value2)
{
return (value1 * value2);
}
//Function for the division of two number
int Div (int value1, int value2)
{
int value3;
value3 = value1/value2;
return value3;
}
int main()
{
int ret_value =0;
//Declaration of array of function pointers
int (*fpatr[4])(int,int) ={Add,Sub,Mul, Div};
//With the help of index of array, calling addition function 'Add'
ret_value = (*fpatr[0])(30,10);
//Prints the output of addition of two numbers
printf ("Addition of two numbers =%d\n\n", ret_value);
//With the help of index of array, calling subtraction function 'Sub'
ret_value = (*fpatr[1])(30,10);
//Prints the output of Subtraction of two numbers
printf ("Subtraction of two numbers =%d\n\n", ret_value);
//With the help of index of array, calling multiplication function 'Mul'
ret_value = (*fpatr[2])(30,10);
//Prints the output multiplication of two numbers
printf ("Multiplication of two numbers =%d\n\n", ret_value);
//With the help of index of array, calling division function 'Div'
ret_value = (*fpatr[3])(30,10);
//Prints the output of division of two numbers
printf ("Division of two numbers =%d\n\n", ret_value);
return 0;
}

Output
Addition of two numbers = 40
Subtraction of two numbers = 20
Multiplication of two numbers = 300
Division of two numbers = 3

Use of typedef with the Function Pointer

Typedef makes the declaration of function pointer in C easy. For instance, In the above C program, declaration of the array of function pointers makes the program a little bit complex. But typedef can make the programming easier.

//typedef of the array of function pointers,
typedef int (*fpatr[4])(int,int);
'fpatr' store the address of four functions.
fpatr operations = {Add, Sub, Mul, Div};

Declare Function Pointers in the Structure

C does not have member functions like C++. In the C programming language, structure only contains data, not function. With the help of function pointer, a structure can contain functions. For instance,

struct fpatr
{
int value;
void (*fpk) (char *data1, int data2);
int (*fpdm) (char* data3);
}End;

Function Pointer as Callback Function

In the operating system (like windows), KMDF (Kernel Mode Driver) use various callback function for the play, and device preparation. This callback function is used for particular events, but we should register this function with the help of function pointer in C.

When an executable code is passed as an argument is known as a callback. In general, if reference of a function is passed to another function as an argument to call function, then it will be called as a callback function.

In the C language, a callback function is called through a function pointer.

C program demonstrates the working of the callback function.
#include <stdio.h>
#include <stdlib.h>
int add (int value1, int value2)
{
int value3;
value3 = value1+value2;
printf ("Addition of two number is %d\n", value3);
}
int CB_fun (int(*ptr) (int,int))
{
(*ptr)(20,10); //Callback to add
}
int main()
{
int value1, value2;
int (*ptr)(int,int) = &add;
//Calling function CB_fun and passing address of the add function as an argument
CB_fun(ptr);
return 0;
}

Output
Addition of two number is 30
A Detail Guide of Function Pointer in C with programming example A Detail Guide of Function Pointer in C with programming example Reviewed by Glory on 21:02 Rating: 5

No comments:

Powered by Blogger.