How to use a function pointer in C

C Function Pointer

In C programming, a function pointer is a pointer that points to a function.

Function pointer stores the address of a function.

Function pointer is used to pass a function as an argument to another function, create callback functions, or implement a dynamic function call mechanism.

Function pointer's return type and parameter type must match with the function's return type and parameter type.

Create a Simple Function Pointer

The following example demonstrates how to create a simple function pointer and use the function pointer to call a function:

Example of function pointer
Output of the above example
The sum of two numbers: 75

Explanation of code:

  • int addNumber(int a, int b) { return a + b;}: This line defines a simple function that takes two integer parameters (int a, int b) and returns their sum as interger type.
  • int (*func_ptr)(int, int);: This line declares a function pointer variable named '*func_ptr' (an asterisk '*' is used before the variable name) and the variable is placed inside parentheses. The pointer takes two integer parameter (int, int) and returns integer type. Here 'func_ptr' pointer's return type and parameter type match with 'addNumber' function's return type and parameter type.
  • func_ptr = &addNumber;: This line assigns the address of the 'addNumber' function to the pointer 'func_ptr'.
  • int result = func_ptr(55, 20); This line call the function 'addNumber' using the function pointer 'func_ptr' passing 55 and 20 as arguments and assigns the result of the function to the variable 'result'.
  • printf("The sum of two numbers: %d\n", result); : This line prints the result (sum of 55 and 20).

Pointing to Multiple Functions

A function pointer can point to multiple functions if the pointer's return type and parameter type match with the function's return type and parameter type.

The following example demonstrates how a function pointer can point to multiple functions:

Example of pointing to multiple functions
Output of the above example
The Result of sum: 75
The result of subtraction: 35

Explanation of code:

  • int addNumber(int a, int b) { return a + b;}: This line defines a simple function that takes two integer parameters (int a, int b) and returns their sum as interger type.
  • int subtractNumber(int a, int b) { return a + b;}: This line defines a simple function that takes two integer parameters (int a, int b) and returns the difference of two interger numbers.
  • int (*func_ptr)(int, int);: This line declares a function pointer variable named '*func_ptr' (an asterisk '*' is used before the variable name) and the variable is placed inside parentheses. The pointer takes two integer parameter (int, int) and returns integer type. Here 'func_ptr' pointer's return type and parameter type match with 'addNumber' function's return type and parameter type.
  • func_ptr = &addNumber;: This line assigns the address of the 'addNumber' function to the pointer 'func_ptr'.
  • int addResult = func_ptr(55, 20); This line call the function 'addNumber' using the function pointer 'func_ptr' passing 55 and 20 as arguments and assigns the result of the function to the variable 'addResult'.
  • printf("The Result of sum: %d\n", addResult);: This line prints the output of the 'addNumber' function (sum of 55 and 20).
  • func_ptr = &subtractNumber;: This line assigns the address of the 'subtractNumber' function to the pointer 'func_ptr'.
  • int subtractResult = func_ptr(55, 20); This line call the function 'subtractNumber' using the function pointer 'func_ptr' passing 55 and 20 as arguments and assigns the result of the function to the variable 'subtractResult'.
  • printf("The result of subtraction: %d\n", subtractResult); : This line prints the output of the 'subtractNumber' function (subtraction of 55 and 20).

Assigning an Array of Functions to a Function Pointer

An array of functions can be assigned to a function pointer as demonstrated in the following example:

Example of assigning an array of functions to a function pointer
Output of the above example
The Result of sum: 75
The result of multiplication: 1100

Explanation of code:

  • int addNumber(int a, int b) { return a + b;}: This line defines a simple function named 'addNumber' that takes two integer parameters (int a, int b) and returns their sum as interger type.
  • int subtractNumber(int a, int b) { return a - b;}: This line defines a simple function named 'subtractNumber' that takes two integer parameters (int a, int b) and returns the difference of two interger numbers.
  • int multiplyNumber(int a, int b) { return a * b;}: This line defines a simple function named 'multiplyNumber' that takes two integer parameters (int a, int b) and returns the product of 'a' and 'b' as interger type.
  • int (*func_ptr[3])(int, int) = { addNumber, subtractNumber, multiplyNumber}; : This line declares a function pointer variable named '*func_ptr' (an asterisk '*' is used before the variable name) and the variable is placed inside parentheses. The pointer takes two integer parameters (int, int) and returns integer type. An arrary of functions '{ addNumber, subtractNumber, multiplyNumber}' is assigned to the pointer variable. Please note that the number of elements are also specified in the pointer using '[3]'.
  • int addResult = func_ptr[0](55, 20); This line calls the first function 'addNumber' using the function pointer 'func_ptr[0]' by passing 55 and 20 as arguments and assigns the result of the function to the variable 'addResult'.
  • printf("The result of first function: %d\n", addResult);: This line prints the output of the 'addNumber' function (sum of 55 and 20).
  • int multiplyResult = func_ptr[2](55, 20); This line calls the third function 'multiplyNumber' using the function pointer 'func_ptr[2]' by passing 55 and 20 as arguments and assigns the result of the function to the variable 'multiplyResult'.
  • printf("The result of third function: %d\n", multiplyResult); : This line prints the output of the 'multiplyNumber' function (product of 55 and 20).

Using Function Pointers as Function Parameters

A function pointer can be used to pass functions as parameters of another function as demonstrated in the following example:

Example of using function pointers as function parameters
Output of the above example
The result of the arithmetic operation: 25 
The result of the arithmetic operation: 5 
The result of the arithmetic operation: 150

Explanation of code:

  • int addNumber(int a, int b) { return a + b;}: This line defines a simple function named 'addNumber' that takes two integer parameters (int a, int b) and returns their sum as interger type.
  • int subtractNumber(int a, int b) { return a - b;}: This line defines a simple function named 'subtractNumber' that takes two integer parameters (int a, int b) and returns the difference of two interger numbers as integer type.
  • int multiplyNumber(int a, int b) { return a * b;}: This line defines a simple function named 'multiplyNumber' that takes two integer parameters (int a, int b) and returns the product of 'a' and 'b' as interger type.
  • int arithmeticOperations( int (*operate_ptr)(int, int), int a, int b){ int result = operate_ptr(a, b); printf("The result of the arithmetic operation: %d \n", result);}: This code defines a function named 'arithmeticOperations' that takes three parameters: a pointer int (*operate_ptr)(int, int) and two integer parameters int a, int b.
    Inside the function, the code 'int result = operate_ptr(a, b);' calls the pointer 'operate_ptr' with 'a' and 'b' as arguments, and the result is stored in the variable 'result'.
    The code 'printf("The result of the arithmetic operation: %d \n", result);' prints the result in the console.
  • arithmeticOperations(addNumber, 15, 10); This line calls the 'arithmeticOperations' function with the function 'addNumber' and the arguments (15 and 10). This call to 'arithmeticOperations' function performs the arithmatic operation of 'addNumber' function and prints the result.
  • arithmeticOperations(subtractNumber, 15, 10); This line calls the 'arithmeticOperations' function with the function 'subtractNumber' and the arguments (15 and 10). This call to 'arithmeticOperations' function performs the arithmatic operation of 'subtractNumber' function and prints the result.
  • arithmeticOperations(multiplyNumber, 15, 10); This line calls the 'arithmeticOperations' function with the function 'multiplyNumber' and the arguments (15 and 10). This call to 'arithmeticOperations' function performs the arithmatic operation of 'multiplyNumber' function and prints the result.

Live Code Playground

In the following C code editor, you can practice by creating more pointers pointing to functions. Click the 'Execute Code' button to execute the code; the executed output will be displayed in the following C Code Output frame. Practice until you become comfortable and proficient with your code.

C logo C Online Editor
C Code Output