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
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
5. Array of Function Pointers
Instead of writing a massiveswitch-case statement for a calculator, you can use an array of function pointers!
c
6. Using typedef for Cleaner Syntax
Because void (*ptr)(int, float) is ugly, C programmers use typedef to create an alias.
c
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 anint*. 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 anint. The compiler will throw a warning or error.
9. Exercises
-
1.
Write a function
sort()that takes an array and a function pointercompare(). Use it to sort an array in both ascending and descending order by passing different compare functions.
-
2.
Use
typedefto 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?
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?
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 (likeqsort).