Jump Tables in C

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:

advertisement
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.

Free 30-Day C Certification Bootcamp is Live. Join Now!

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.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.