Chapter 35Advanced

Function Pointers in C

Store functions in variables! Pass functions as arguments, create callbacks, and build flexible code.

18 min readUpdated 2024-12-16
function pointercallbackqsorttypedeffunction array

What You Will Learn

  • Declare function pointers
  • Pass functions as arguments
  • Create callback functions
  • Use function pointer arrays

01What is a Function Pointer?

🎯 Simple Definition

A function pointer is a variable that stores the address of a function. You can use it to call functions dynamically!

🤔 Why Use Function Pointers?

Callbacks

Pass functions as arguments

Dynamic behavior

Choose function at runtime

Event handlers

React to events

Plugin systems

Extensible architecture

02Function Pointer Syntax

📝 Declaration Syntax

return_type (*pointer_name)(parameter_types);

The parentheses around *pointer_name are important!

function_pointer_basic.c
C
1#include <stdio.h>
2
3// A simple function
4int add(int a, int b) {
5 return a + b;
6}
7
8int subtract(int a, int b) {
9 return a - b;
10}
11
12int main() {
13 // Declare a function pointer
14 // It can point to any function that:
15 // - Returns int
16 // - Takes two int parameters
17 int (*operation)(int, int);
18
19 // Point to 'add' function
20 operation = add; // or: operation = &add;
21
22 // Call through pointer
23 int result = operation(10, 5);
24 printf("add(10, 5) = %d\n", result);
25
26 // Point to 'subtract' function
27 operation = subtract;
28
29 result = operation(10, 5);
30 printf("subtract(10, 5) = %d\n", result);
31
32 return 0;
33}
Output

add(10, 5) = 15

subtract(10, 5) = 5

03Callback Functions

📞 What is a Callback?

A callback is a function passed to another function as an argument. The receiving function can then "call back" to it!

callback.c
C
1#include <stdio.h>
2
3// Callback function type for processing array elements
4void applyToArray(int* arr, int size, int (*callback)(int)) {
5 for (int i = 0; i < size; i++) {
6 arr[i] = callback(arr[i]); // Apply callback to each element
7 }
8}
9
10// Functions to use as callbacks
11int doubleValue(int x) {
12 return x * 2;
13}
14
15int square(int x) {
16 return x * x;
17}
18
19int addTen(int x) {
20 return x + 10;
21}
22
23void printArray(int* arr, int size) {
24 for (int i = 0; i < size; i++) {
25 printf("%d ", arr[i]);
26 }
27 printf("\n");
28}
29
30int main() {
31 int numbers[] = {1, 2, 3, 4, 5};
32 int size = 5;
33
34 printf("Original: ");
35 printArray(numbers, size);
36
37 // Apply 'double' callback
38 applyToArray(numbers, size, doubleValue);
39 printf("Doubled: ");
40 printArray(numbers, size);
41
42 // Reset and apply 'square'
43 int nums2[] = {1, 2, 3, 4, 5};
44 applyToArray(nums2, size, square);
45 printf("Squared: ");
46 printArray(nums2, size);
47
48 // Reset and apply 'addTen'
49 int nums3[] = {1, 2, 3, 4, 5};
50 applyToArray(nums3, size, addTen);
51 printf("Add 10: ");
52 printArray(nums3, size);
53
54 return 0;
55}
Output

Original: 1 2 3 4 5

Doubled: 2 4 6 8 10

Squared: 1 4 9 16 25

Add 10: 11 12 13 14 15

04Array of Function Pointers

function_pointer_array.c
C
1#include <stdio.h>
2
3int add(int a, int b) { return a + b; }
4int subtract(int a, int b) { return a - b; }
5int multiply(int a, int b) { return a * b; }
6int divide(int a, int b) { return b != 0 ? a / b : 0; }
7
8int main() {
9 // Array of function pointers
10 int (*operations[4])(int, int) = {add, subtract, multiply, divide};
11 char* names[] = {"Add", "Subtract", "Multiply", "Divide"};
12
13 int a = 20, b = 5;
14
15 printf("Calculator Demo (a=%d, b=%d):\n\n", a, b);
16
17 for (int i = 0; i < 4; i++) {
18 int result = operations[i](a, b); // Call through array
19 printf("%s: %d\n", names[i], result);
20 }
21
22 // Menu-driven selection
23 printf("\nEnter operation (0=Add, 1=Sub, 2=Mul, 3=Div): ");
24 int choice = 0; // Simulating user input
25 printf("%d\n", choice);
26
27 printf("Result: %d\n", operations[choice](a, b));
28
29 return 0;
30}
Output

Calculator Demo (a=20, b=5):

Add: 25

Subtract: 15

Multiply: 100

Divide: 4

05Cleaner Syntax with typedef

Function pointer syntax is complex. Use typedef to make it cleaner!

Without typedef (hard to read):

int (*operation)(int, int);

With typedef (clean):

Operation op;

typedef_function_pointer.c
C
1#include <stdio.h>
2
3// Define a type for our function pointer
4typedef int (*Operation)(int, int);
5
6int add(int a, int b) { return a + b; }
7int multiply(int a, int b) { return a * b; }
8
9// Now function parameters are clean!
10int calculate(int a, int b, Operation op) {
11 return op(a, b);
12}
13
14int main() {
15 // Clean variable declaration
16 Operation myOp = add;
17 printf("add: %d\n", myOp(10, 5));
18
19 myOp = multiply;
20 printf("multiply: %d\n", myOp(10, 5));
21
22 // Pass to function easily
23 printf("calculate with add: %d\n", calculate(10, 5, add));
24 printf("calculate with multiply: %d\n", calculate(10, 5, multiply));
25
26 return 0;
27}
Output

add: 15

multiply: 50

calculate with add: 15

calculate with multiply: 50

06Summary

🎯 Key Takeaways

  • Function pointer: Stores address of a function
  • Syntax: return_type (*name)(params)
  • Callbacks: Pass functions as arguments
  • typedef: Makes syntax cleaner
  • Arrays: Store multiple function pointers

09Next Steps

Learn about bit manipulation techniques: