C Notes
Learn function pointers in C programming with syntax, callbacks, qsort usage, arrays of function pointers, and practical examples for building flexible programs.
Function pointers are one of the most powerful features in C programming. They allow you to store the address of a function in a variable, pass functions as arguments to other functions, and even build dispatch tables. If you've ever wondered how callback mechanisms work or how qsort() sorts any data type, function pointers are the answer.
In this guide, we'll start from the basics and work our way up to advanced patterns like arrays of function pointers and building plugin architectures.
What is a Function Pointer?
A function pointer is a variable that stores the memory address of a function. Just like a regular pointer holds the address of a data variable, a function pointer holds the address of code. When you call a function through its pointer, the program jumps to that address and executes the code there.
Every function in C has an address in memory. When you write the function name without parentheses, it evaluates to that address.
#include <stdio.h>
void greet(void) {
printf("Hello, World!\n");
}
int main() {
printf("Address of greet: %p\n", (void*)greet);
return 0;
}Address of greet: 0x401136
Function Pointer Syntax
The syntax for declaring a function pointer can look intimidating at first, but once you understand the pattern, it becomes second nature.
return_type (*pointer_name)(parameter_types);Here's the breakdown:
return_type– the return type of the function being pointed to(*pointer_name)– the parentheses around the asterisk and name are crucial; they indicate this is a pointer to a function(parameter_types)– the parameter list of the target function
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}
int main() {
// Declare a function pointer
int (*operation)(int, int);
// Assign the address of add
operation = add;
printf("Addition: %d\n", operation(10, 5));
// Reassign to subtract
operation = subtract;
printf("Subtraction: %d\n", operation(10, 5));
return 0;
}Addition: 15 Subtraction: 5
Calling Conventions
There are two ways to call a function through a pointer – both are valid in C:
// Method 1: Direct call (modern style)
result = operation(10, 5);
// Method 2: Explicit dereference (older style)
result = (*operation)(10, 5);Both produce identical results. The first style is more common in modern code.
Callback Functions in C
A callback is a function passed as an argument to another function, which then "calls back" the passed function at the appropriate time. This is the most common real-world use of function pointers.
--- Doubling --- Double: 2 Double: 4 Double: 6 Double: 8 Double: 10 --- Squaring --- Square: 1 Square: 4 Square: 9 Square: 16 Square: 25
Using qsort() with Function Pointers
The C standard library function qsort() is a perfect example of function pointers in action. It can sort any data type because it accepts a comparison function as an argument.
Sorted ascending: 3 17 21 42 56 89 Sorted descending: 89 56 42 21 17 3 Sorted names: Alice Bob Charlie Diana
Arrays of Function Pointers
An array of function pointers lets you create dispatch tables – selecting a function based on an index. This pattern is commonly used in menu-driven programs, state machines, and command processors.
Addition: 25.00 Subtraction: 15.00 Multiplication: 100.00 Division: 4.00
Using typedef for Cleaner Code
Function pointer syntax can get complex. Using typedef makes your code much more readable:
[LOG] Event: user_login [ALERT] Critical: disk_full
Function Pointers in Structures
Embedding function pointers in structures lets you simulate object-oriented behavior – giving structs their own "methods."
Name: Rahul, Age: 22 Is adult: Yes
Common Comparison Table
| Feature | Regular Pointer | Function Pointer |
|---|---|---|
| Stores | Address of data | Address of code |
| Dereference | Accesses data | Calls function |
| Arithmetic | Supported | Not supported |
| sizeof | Size of data type | Platform-specific |
| Casting | To other data pointers | To other function pointers |
| NULL check | Recommended | Essential |
Interview Questions on Function Pointers
Q1: What happens if you call a NULL function pointer? It causes undefined behavior – typically a segmentation fault. Always check for NULL before calling through a function pointer.
Q2: Can a function pointer point to a function with a different signature? Technically you can cast it, but calling through an incompatible function pointer is undefined behavior. The signatures must match.
**Q3: What is the difference between void *func() and void (*func)()?** void *func() declares a function named func that returns a void*. void (*func)() declares a pointer to a function that returns void.
Q4: How are function pointers used in implementing polymorphism in C? By embedding function pointers in structures, different instances can point to different function implementations, mimicking virtual methods in C++.
**Q5: Why does qsort use const void* parameters in its comparison function?** Because qsort is generic – it doesn't know the data type. The void* allows any pointer type, and const ensures the comparison function doesn't modify the data.
Summary
Function pointers are essential for writing flexible, reusable C code. They enable callback mechanisms, generic algorithms like qsort(), dispatch tables, event systems, and even basic polymorphism. While the syntax takes getting used to, mastering function pointers opens up powerful design patterns that make your C programs modular and extensible. Remember to always use typedef for complex function pointer types and always validate pointers before calling through them.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Function Pointers in C – Complete Guide with Examples.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, advanced, function, pointers, function pointers in c – complete guide with examples
Related C Programming Topics