Calling Function Pointers
Part of the Object Oriented Programming section of Coddy's C journey — lesson 34 of 61.
Now that you know how to declare a function pointer and assign a function to it, the next step is actually calling the function through that pointer.
The syntax is straightforward — you use the function pointer just like you would call a regular function:
int add(int a, int b) {
return a + b;
}
int main() {
int (*operation)(int, int) = add;
int result = operation(5, 3); // Calls add(5, 3)
printf("%d\n", result); // Output: 8
return 0;
}Simply write the pointer name followed by parentheses containing the arguments. C automatically dereferences the pointer and executes the function it points to.
You can also use explicit dereference syntax with (*operation)(5, 3), but it's optional and less common. Both forms produce identical results.
The real power emerges when you reassign the pointer to different functions:
int subtract(int a, int b) {
return a - b;
}
operation = subtract;
result = operation(5, 3); // Now calls subtract(5, 3)
printf("%d\n", result); // Output: 2The same variable operation now executes completely different logic. This ability to swap behavior at runtime is the foundation of polymorphism in C.
Challenge
EasyLet's practice calling functions through pointers and swapping which function gets executed. You'll build a simple calculator that can switch between addition and subtraction at runtime.
Write a program that:
- Defines two functions:
add— takes twointparameters and returns their sumsubtract— takes twointparameters and returns the first minus the second
- In
main, declares a function pointer calledcalcthat can point to functions taking twointparameters and returning anint - Reads four inputs: two integers and two operation codes
- For each operation code:
- Assign the appropriate function to
calc(1 for add, 2 for subtract) - Call the function through the pointer with the two numbers
- Print the result
- Assign the appropriate function to
You will receive four inputs: the first number, the second number, the first operation code (1 or 2), and the second operation code (1 or 2).
Your output should show two results on separate lines—one for each operation performed using the same two numbers:
15
5In this example, with inputs 10, 5, 1, 2: the first operation (code 1) adds 10 + 5 = 15, then the pointer is reassigned, and the second operation (code 2) subtracts 10 - 5 = 5.
This demonstrates how the same pointer variable can execute completely different logic by reassigning it between calls.
Cheat sheet
To call a function through a pointer, use the pointer name followed by parentheses with arguments:
int add(int a, int b) {
return a + b;
}
int main() {
int (*operation)(int, int) = add;
int result = operation(5, 3); // Calls add(5, 3)
printf("%d\n", result); // Output: 8
return 0;
}You can also use explicit dereference syntax (*operation)(5, 3), but it's optional.
Function pointers can be reassigned to point to different functions at runtime:
int subtract(int a, int b) {
return a - b;
}
operation = subtract;
result = operation(5, 3); // Now calls subtract(5, 3)
printf("%d\n", result); // Output: 2This allows the same pointer variable to execute different logic by reassigning it between calls.
Try it yourself
#include <stdio.h>
// TODO: Define the add function here
// TODO: Define the subtract function here
int main() {
// Read inputs
int num1, num2, op1, op2;
scanf("%d", &num1);
scanf("%d", &num2);
scanf("%d", &op1);
scanf("%d", &op2);
// TODO: Declare a function pointer called 'calc' that can point to
// functions taking two int parameters and returning an int
// TODO: For the first operation code (op1):
// - Assign the appropriate function to calc (1 for add, 2 for subtract)
// - Call the function through the pointer and print the result
// TODO: For the second operation code (op2):
// - Reassign calc to the appropriate function
// - Call the function through the pointer and print the result
return 0;
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Object Oriented Programming
1Modular Programming Basics
Header FilesInclude GuardsSource FilesStatic FunctionsRecap: Modular Calculator4Encapsulation
Opaque Pointers ConceptDefining Opaque StructsGetters and SettersValidation in SettersRecap: Secret Box7Function Pointers
Declaring Function PointersCalling Function PointersTypedef for Function PointersPassing Functions as ArgumentsRecap: Calculator Dispatch2Objects and Methods
Structs as ObjectsThe 'Self' PointerConst CorrectnessPointer vs ValueHelper MethodsRecap: Point Manager5Project: Simple Bank Account
Project SetupImplementation of Account3Object Lifecycle
Constructor PatternDestructor PatternStack InitializationDeep CopyRecap: String Wrapper6Inheritance via Composition
Struct EmbeddingThe First Member RuleAccessing Parent MembersUpcastingRecap: Shape Hierarchy