What is a Jump Table in C?
A jump table is essentially an array of function pointers. Each element in the array points to a function, and you can call a function by indexing into the array. This method allows for dynamic function calls based on runtime conditions, offering a cleaner and often faster alternative to lengthy if-else or switch statements.
Why Use Jump Tables?
Here are some common reasons to use jump tables:
- Faster Execution: Eliminates multiple condition checks.
- Cleaner Code: Reduces clutter from long switch-case or if-else blocks.
- Better Maintainability: Easy to add or update functionality without modifying control flow logic.
Syntax of Jump Table in C
return_type (*function_ptr_array[])() = {function1, function2, ..., functionN};
- return_type: The return type of the functions.
- function_ptr_array: The array of function pointers.
- function1…N: Function names already defined.
Implementing Jump Tables in C
// Define a set of functions void quiz() { printf("Quiz Function\n"); } void test() { printf("Test Function\n"); } void mcqs() { printf("MCQs Function\n"); } // Create a jump table void (*jumpTable[])() = { quiz, test, mcqs }; // Call: jumpTable[index]() jumpTable[1](); // Calls test()
Jump Table Example
#include <stdio.h> // Define functions void quizModule() { printf("Running Quiz Module\n"); } void testModule() { printf("Running Test Module\n"); } void certification() { printf("Launching Certification...\n"); } void mcqPractice() { printf("MCQ Practice Started\n"); } int main() { // Jump table void (*dispatch[])() = { quizModule, testModule, certification, mcqPractice }; int userChoice; printf("Enter module (0=Quiz, 1=Test, 2=Cert, 3=MCQs): "); scanf("%d", &userChoice); if (userChoice >= 0 && userChoice < 4) dispatch[userChoice](); // Jump to corresponding function else printf("Invalid module selected.\n"); return 0; }
Output:
Enter module (0=Quiz, 1=Test, 2=Cert, 3=MCQs): 2 Launching Certification...
This C program uses a jump table—an array of function pointers—to call different modules like quizModule(), testModule(), certification(), and mcqPractice() based on user input. Instead of using multiple if or switch statements, the program directly jumps to the function using the input index. If the input is valid (0–3), it runs the chosen module; otherwise, it shows an error. This method makes the code cleaner, faster, and easier to extend.
Compiler Optimization with Jump Tables
Modern compilers can optimize switch statements into jump tables when the case values are densely packed. This optimization allows for constant-time branching, improving performance.​
However, if the case values are sparse or non-sequential, compilers may opt for other strategies like binary search or chained if-else statements.​
When to Use Jump Tables
- State Machines: Implementing state transitions where each state corresponds to a function.​
- Menu-Driven Programs: Handling user choices in command-line interfaces.​​
- Command Parsers: Mapping commands to their respective handler functions.​​
- Event Handling: Dispatching events to appropriate handlers based on event type.​
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Practice Computer Science MCQs
- Watch Advanced C Programming Videos
- Practice BCA MCQs
- Apply for C Internship
- Check Computer Science Books