Function Pointers in C 1722521727
Function Pointers in C 1722521727
com/ 2024-08-01
Function Pointers in C
Function pointers are an advanced feature in C programming that allow you to store the address of a function
in a pointer variable. They enable flexible and efficient code, especially useful for callbacks, dynamic function
calls, and implementing state machines. This blog post will cover the declaration, definition, and various uses
of function pointers, with detailed examples to illustrate each concept.
Table of Contents
1. What is a Function Pointer?
2. Declaring Function Pointers
3. Defining and Assigning Function Pointers
4. Using Function Pointers
To declare a function pointer, you specify the return type, followed by an asterisk *, the pointer name, and the
parameter list enclosed in parentheses.
Syntax:
return_type (*pointer_name)(parameter_list);
Example:
This declares a pointer named operation that can point to any function taking two int parameters and
returning an int.
After declaring a function pointer, you can assign it the address of a function using the function's name
without parentheses.
Example:
1/7
28_function_pointers_in_c.md https://embeddedforall.com/ 2024-08-01
#include <stdio.h>
// Function declaration
int add(int a, int b)
{
return a + b;
}
int main()
{
// Function pointer declaration
int (*operation)(int, int);
return 0;
}
output:
Result: 8
Callbacks
Callbacks are functions passed as arguments to other functions. They are commonly used in event-driven
programming and asynchronous operations.
Example:
#include <stdio.h>
// Callback function
void greet(const char *name)
{
printf("Hello, %s!\n", name);
}
2/7
28_function_pointers_in_c.md https://embeddedforall.com/ 2024-08-01
int main()
{
// Using the callback
execute(greet, "World");
return 0;
}
Function pointers allow selecting and calling functions dynamically at runtime based on certain conditions.
Example:
#include <stdio.h>
int main()
{
// Function pointer declaration
int (*operation)(int, int) = NULL;
char op;
3/7
28_function_pointers_in_c.md https://embeddedforall.com/ 2024-08-01
printf("invalid operation");
}
return 0;
}
output:
Operation +/-: -
Result: 2
-----------
Operation +/-: +
Result: 8
-----------
Operation +/-: =
invalid operation
State Machines
State machines can be efficiently implemented using function pointers, making the code more modular and
maintainable.
Example:
#include <stdio.h>
// State functions
void stateA()
{
printf("In State A\n");
}
void stateB()
{
printf("In State B\n");
}
void stateC()
{
printf("In State C\n");
}
4/7
28_function_pointers_in_c.md https://embeddedforall.com/ 2024-08-01
int main()
{
// Array of function pointers
void (*state[])(void) = {stateA, stateB, stateC};
return 0;
}
output:
In State A
In State B
In State C
An array of function pointers allows storing multiple function addresses in a single data structure, which is
useful for implementing tables of functions.
Example:
#include <stdio.h>
// Function declarations
void add(int a, int b) {
printf("Add: %d\n", a + b);
}
int main() {
// Array of function pointers
void (*operations[2])(int, int) = { add, subtract };
return 0;
}
5/7
28_function_pointers_in_c.md https://embeddedforall.com/ 2024-08-01
output:
Add: 8
Subtract: 2
Storing function pointers in structures allows for more complex data structures, useful for object-oriented
programming patterns.
Example:
#include <stdio.h>
int main()
{
Operation op;
op.operation = subtract;
op.operation(5, 3);
return 0;
}
output:
Add: 8
Subtract: 2
6/7
28_function_pointers_in_c.md https://embeddedforall.com/ 2024-08-01
Function pointers in C are a powerful feature that allows for dynamic function calls, flexible program design,
and efficient code management. By understanding how to declare, define, and use function pointers, you can
enhance your programming skills and write more versatile C programs. Experiment with the examples
provided and explore advanced uses to master function pointers in C.
Veeresh P S: https://www.linkedin.com/in/veeresh-p-s/
Embedded for All: https://embeddedforall.com/
Blog: https://embeddedforall.com/blog-2/
Youtube: https://www.youtube.com/@embeddedforallefa
7/7