在C中跳转表示例

use*_*265 5 c jump-table

请给我一些跳转表用法的例子.我在维基百科上看过这个例子:

#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]();
}
Run Code Online (Sandbox Code Playgroud)

但我想知道是否有另一种方法来调用函数而不是使用索引,如上所示,在上面的例子中 jump_table[value]();

我想要实现的是,而不是使用索引是否有一种方法来使用函数本身的名称.

例如,假设我们在结构中有所有函数指针.

typedef struct _funcptrs
{
  void func1();
  void func2();
} funcptrs;
Run Code Online (Sandbox Code Playgroud)

现在,当我想调用该函数时,我可以做些什么funcptrs.func1()吗?

Jer*_*fin 9

你当然可以创建一个struct包含指向函数的指针.甚至有充分的理由这样做.

例如,考虑操作系统和某种设备驱动程序之间的接口.简化了很多,这可能看起来像这个顺序:

struct device { 
    int (*open)(unsigned mode);
    int (*close)(void);
    int (*read)(void *buffer, size_t size);
    int (*write)(void *buffer, size_t size);
};
Run Code Online (Sandbox Code Playgroud)

然后,单个设备驱动程序将创建此类型的结构,并初始化各个指针以引用与特定设备相关的功能:

struct device serial_port = { 
    open_serial,
    close_serial,
    read_serial,
    write_serial
};

struct device ethernet_adapter = { 
    open_net,
    close_net,
    read_net,
    write_net
};

struct device keyboard = { 
    open_keyboard,
    close_keyboard,
    read_keyboard,
    NULL  // we'll assume no writing to the keyboard...
};
Run Code Online (Sandbox Code Playgroud)

然后,某些更高级别的功能可以接收其中一个,并打开/关闭/读取/写入某些设备,而无需知道所涉及设备的确切身份.当然,对于真正的操作系统来说,它比这更复杂,但一般的想法(或者至少可以)非常相似.

  • +1 - 这与我认为OP所要求的非常接近。在 80 年代,当我们渴望 C++ 虚拟函数时,这是一种常见的技术,但 PC 上只有 C。 (2认同)

nos*_*nos 6

当然,但您需要将它们声明为函数指针并首先初始化它们.虽然如果你必须拼出功能名称,这会破坏跳转表的目的.

例如

#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;
}
Run Code Online (Sandbox Code Playgroud)

如果需要通过将函数名称作为字符串来调用函数,则需要在函数名称和函数指针之间创建映射,然后在表中搜索该函数,并调用它.

#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;
}
Run Code Online (Sandbox Code Playgroud)