Embedded Development Board Learning


Function pointers

Date: agosto 25, 2025

Author: Guillermo Garcia

Categories: Embedded C programming Tags:

In this article, we will see how to use Function pointers. This feature is widely used in state machines used in programs for embedded systems.

Function pointers

Like variables, functions need to be stored in memory. Therefore, also functions have a starting address. In C, it is possible to declare a pointer that points to the starting address of a function.

As an example we will write a program with a main and two functions where one of the functions is called by the use of a function pointer.

#include <stdio.h>

 float OneThird(float);
 float OneFifth(float);

 int main()
{
 float(*pf)(float);
 pf = OneThird;
 printf("%f\n", (*pf)(3.0));
 return 0;
}

float OneThird(float x)
{
 return x / 3;
}

float OneFifth(float x)
{
 return x / 5;
} 

The line

float (*pf) (float); 

declares a variable pf that is a pointer (*pf) to a function that receives a float as parameter and returns a float. The parentheses around *pf are needed to indicate that pf is a pointer.

Without those parentheses, the instruction would become:

float * pf (float);

which declares a function with name pf that receives a float as argument and has a pointer to a float as return value.

The next statement:

pf = OneThird;

assigns the starting address of the function with name OneThird to the pointer pf. Like arrays, the name of a function refers to the starting address of that function in memory.

To use the function the pointer is pointing to, the pointer needs to be dereferenced. On top the correct function arguments need to be passed:

printf("%f\n", (*pf)(3.0));

Array of function pointers

Function pointers are commonly used in programs where a user can select a function to be carried out from a list of options. Using an array of function pointers to all possible functions, the user’s choice can be translated into an index that allows to select the correct function pointer from the array.

#include <stdio.h>

 int add(int, int);
 int subtract(int, int);

int main(void)
{
 int a, choice;
 int(*fptr[2])(int, int) = { add, subtract };

 printf("Enter your choice:\n");
 printf("\t0:\taddition (10 + 2)\n\t1:\tsubtraction (10 - 2)\n");
 scanf("%d%*c", &choice);

 a=(*fptr[choice])( 10, 2);

 printf("The requested operation gives: %d\n", a);
 return 0;
}

int add(int x, int y)
{
 return x + y;
}

int subtract(int x, int y)
{
 return x - y;
}

If the users chooses option 0 (addition), the element with index 0 is selected in the array, resulting in a function pointer to the function with name add.

Function pointers as function argument

Finally, function pointers allow using functions as parameters of other functions.

#include <stdio.h>
 
int add(int, int);
int subtract(int, int);
int domath(int(*)(int, int), int, int);

int main(void)
{
 int a, b;

 a = domath(add, 10, 2);
 printf("Add with function pointer gives: %d\n", a);

 b = domath(subtract, 10, 2);
 printf("Subtract with function pointer gives: %d\n", b);

 return 0;
}

int add(int x, int y) 
{
 return x + y;
}

int subtract(int x, int y) 
{
 return x - y;
}

 // run the function pointer with inputs
int domath(int(*mathop)(int, int), int x, int y) 
{
 return (*mathop)(x, y);
}

Output:

Add with function pointer gives: 12
Subtract with function pointer gives: 8

The first argument of the function domath is a function pointer with name mathop that can point to a function that receives two integers as inputs and returns an integer. Calling the function domath with add as first parameter, assigns the function add to the function pointer mathop.

Summary

summary functions


Card image cap
Guillermo Garcia Thanks for reading! I am one of the editors of this site and I am committed to providing you with the best information possible by sharing my experience in embedded systems.


Comments... There are no comments.

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

Publicidad