Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7734327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:13:21+00:00 2026-06-01T07:13:21+00:00

Please give me some examples of jump table usage. I have seen this example

  • 0

Please give me some examples of jump table usage. I have seen this example on wikipedia:

#include <stdio.h>
#include <stdlib.h>

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[4] = {func0, func1, func2, func3};



int main (int argc, char **argv) {
    int value;

    /* Convert first argument to 0-3 integer (Hash) */
    value = atoi(argv[1]) % 4;
    if (value < 0) {
        value *= -1;
    }

    /* Call appropriate function (func0 thru func3) */
    jump_table[value]();
}

But I was wondering if there is an alternate way of calling the function instead of using index as shown, in the above case it is jump_table[value]();

What I want to achieve is, instead of using the index is there a way to use the name of the function itself.

For example, say we have all the function pointers in a struct.

typedef struct _funcptrs
{
  void func1();
  void func2();
} funcptrs;

and now when I want to call the function can I do something like funcptrs.func1() ?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T07:13:22+00:00Added an answer on June 1, 2026 at 7:13 am

    Certainly, but you need to declare them as function pointers and initialize them first. Though this defeats the purpose of a jump table if you have to spell out the function name.

    e.g.

    #include <stdio.h>
    
    void func1 (void) { printf( "1\n" ); }
    void func0 (void) { printf( "0\n" ); }
    
    typedef struct
    {
      void (*func0)(void);
      void (*func1)(void);
    }  funcptrs;
    
    int main(int argc, char *argv[])
    {
       funcptrs funcs = { func0, func1 };
       funcs.func1();
       return 0;
    }
    

    If you need to call the function by having the name of the function as a string, you need to create a mapping between the functions name and a function pointer, then search the table for that function, and call it.

    #include <stdio.h>
    #include <string.h>
    
    void func1 (void) { printf( "1\n" ); }
    void func0 (void) { printf( "0\n" ); }
    
    #define DEFUN(name) { #name, name }
    
    typedef struct
    {
      const char *name;
      void (*func)(void);
    }  funcptrs;
    
    void call(funcptrs *ptrs, const char *name)
    {
        int i;
        for(i = 0; ptrs[i].name; i++) {
          if(strcmp(ptrs[i].name, name) == 0) {
               ptrs[i].func();
               break;
           }
        }
    }
    int main(int argc, char *argv[])
    {
       funcptrs funcs[] = {DEFUN(func0), DEFUN(func1), {NULL,NULL}};
       call(funcs, "func0");
       return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Anybody please give some useful links on this topic.i need to create a content
can someone please give me some examples where enumeration is used? i dont understand
I am very confused between these two consistency models. Please give some timeline examples
What is the difference between Cloud, Cluster and Grid? Please give some examples of
Can someone please give some examples where pass by reference is preferred over pass
please tell me how to work with expandable list view give some examples.If i
Can you please give me some comparison between C compilers especially with respect to
What does $+ in a GNU makefile mean? Also, please give me some good
Please give me a real time example for singleton pattern . Different threads accessing
Please give me an example for MVC pattern used in Java SWING package ?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.