Branch Table - Jump Table Example in C

Jump Table Example in C

Another simple example, this time demonstrating a jump table rather than a mere branch table. This allows program blocks outside of the currently active procedure/function to be called:

#include #include typedef void (*Handler)(void); /* A pointer to a handler function */ /* The functions */ void func3 (void) { printf( "3\n" ); } void func2 (void) { printf( "2\n" ); } void func1 (void) { printf( "1\n" ); } void func0 (void) { printf( "0\n" ); } Handler jump_table = {func0, func1, func2, func3}; int main (int argc, char **argv) { int value; /* Convert first argument to 0-3 integer (Hash) */ value = atoi(argv) % 4; if (value < 0) { value *= -1; } /* Call appropriate function (func0 thru func3) */ jump_table; }

Read more about this topic:  Branch Table

Famous quotes containing the words jump and/or table:

    Well, you look so pretty in it
    Honey, can I jump on it sometime?
    Yes, I just wanna see
    If it’s really that expensive kind
    You know it balances on your head
    Just like a mattress balances
    On a bottle of wine
    Your brand new leopard-skin pill-box hat.
    Bob Dylan [Robert Allen Zimmerman] (b. 1941)

    In this sad state, God Tender Bowells run
    Out streams of Grace: And he to end all strife
    The Purest Wheate in Heaven, his deare-dear Son
    Grinds, and kneads up into this Bread of Life.
    Which Bread of Life from Heaven down came and stands
    Disht on thy Table up by Angells Hands.
    Edward Taylor (1645–1729)