Skip to main content
C Programming Basics
CHAPTER 26 Beginner

Function Pointers

Updated: May 17, 2026
5 min read

# CHAPTER 26

Function Pointers

1. Introduction

So far, we have pointed to integers, structures, and arrays. But did you know you can point a pointer at actual, executable code? A Function Pointer stores the memory address of a function. This allows you to pass functions as arguments to other functions, a concept known as Callbacks.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Declare and initialize a function pointer.
  • Understand the complex syntax of function pointers.
  • Implement Callback functions.
  • Create an array of function pointers.

3. Syntax of Function Pointers

The syntax is notoriously tricky because you have to specify the return type and the parameters of the function you want to point to.

Syntax: returntype (*pointername)(parameter_types);

c
123456789101112131415161718192021
#include <stdio.h>

// A normal function
void sayHello(char *name) {
    printf("Hello, %s!\n", name);
}

int main() {
    // 1. Declare a function pointer
    // It points to a function that returns void and takes a char*
    void (*func_ptr)(char*); 

    // 2. Assign the address of the function to the pointer
    func_ptr = &sayHello; // (or just func_ptr = sayHello; works too)

    // 3. Call the function using the pointer
    (*func_ptr)("Alice"); // Prints: Hello, Alice!
    // func_ptr("Alice"); // This shorthand also works!

    return 0;
}

4. Passing Functions as Arguments (Callbacks)

The real power of function pointers is passing them into other functions. This allows the receiving function to "call back" the function you passed in. This is heavily used in event-driven programming (like clicking a button in a GUI).
c
123456789101112131415161718192021
#include <stdio.h>

// Two simple functions
int add(int a, int b) { return a + b; }
int multiply(int a, int b) { return a * b; }

// A function that TAKES a function pointer as an argument
void executeMath(int x, int y, int (*operation)(int, int)) {
    int result = operation(x, y); // Calling the passed function
    printf("The result is: %d\n", result);
}

int main() {
    printf("Executing Add:\n");
    executeMath(5, 3, add); // Passes the 'add' function

    printf("Executing Multiply:\n");
    executeMath(5, 3, multiply); // Passes the 'multiply' function

    return 0;
}

5. Array of Function Pointers

Instead of writing a massive switch-case statement for a calculator, you can use an array of function pointers!
c
123456789101112131415161718192021
#include <stdio.h>

int add(int a, int b) { return a + b; }
int sub(int a, int b) { return a - b; }
int mul(int a, int b) { return a * b; }

int main() {
    // Array of 3 function pointers
    int (*math_operations[3])(int, int) = {add, sub, mul};
    
    int choice = 1; // 0 for add, 1 for sub, 2 for mul
    int a = 10, b = 5;

    if (choice >= 0 && choice < 3) {
        // Calls sub(10, 5)
        int result = math_operations[choice](a, b);
        printf("Result: %d\n", result);
    }

    return 0;
}

6. Using typedef for Cleaner Syntax

Because void (*ptr)(int, float) is ugly, C programmers use typedef to create an alias.
c
12345
// Creates a new type called "MathFunc"
typedef int (*MathFunc)(int, int);

// Now you can use it like a normal data type!
MathFunc myFunc = add; 

7. Memory-Level Explanation

When a C program is compiled, the machine code for functions is stored in the Text Segment (Code Segment) of memory. A function pointer simply holds the memory address of the first instruction of that function in the Text Segment.

8. Common Mistakes

  • Forgetting the parentheses: int *funcptr(int); declares a normal function that returns an int*. You MUST use parentheses: int (*funcptr)(int); to declare a function pointer.
  • Signature mismatch: Trying to point a void (*)(int) pointer at a function that returns an int. The compiler will throw a warning or error.

9. Exercises

  1. 1. Write a function sort() that takes an array and a function pointer compare(). Use it to sort an array in both ascending and descending order by passing different compare functions.
  1. 2. Use typedef to clean up the function pointer syntax in the callback example above.

10. MCQ Quiz with Answers

Question 1

What does a function pointer store?

Question 2

Which of the following correctly declares a function pointer p that points to a function taking an int and returning a float?

Question 3

What is a Callback function?

Question 4

Where does the code for functions reside in memory?

Q5. Can you have an array of function pointers? a) Yes b) No Answer: a) Yes
Question 6

Which keyword is often used to simplify function pointer syntax?

Question 7

If void printMsg() is a function, how do you assign it to void (*ptr)()?

Question 8

Why use an array of function pointers?

Q9. Can a function pointer point to a macro? a) Yes b) No Answer: b) No (Macros are expanded by the preprocessor before compilation; they don't have memory addresses).
Question 10

What happens if the signature of the function pointer doesn't match the function being pointed to?

11. Interview Questions

  • Q: Explain the syntax difference between a function returning a pointer, and a pointer to a function.
  • Q: How is the qsort() function in the C standard library implemented? (Answer: It uses function pointers for the comparison logic).
  • Q: What is the Text segment, and why are function pointers dangerous if exploited by hackers?

12. Summary

Function Pointers allow C to treat functions as data. They store the memory address of executable code. This enables Callbacks (passing functions as arguments) and arrays of functions, which are vital for event-driven programming and building generic, reusable libraries (like qsort).

13. Next Chapter Recommendation

In Chapter 27: Error Handling in C, we will learn how to detect, manage, and gracefully recover from errors like missing files, bad memory allocation, and math violations.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·