0

我试图了解如何实现函数指针的二维表(以简化半复杂的 switch/case/if 构造)。

以下工作 - 经过一些争论 - 根据需要。

#include <stdio.h>

void AmpEn(void) {printf("AmpEn\n");}
void MovCurColumn1(void) {printf("MovCur\n");}
void AmpLevel(void) {printf("AmpLevel\n");}
void Phase(void) {printf("Phase\n");}

typedef void (*func_ptr)(void);

int main (void) {

    func_ptr table[2][2] = {{AmpEn, MovCurColumn1},{AmpLevel, Phase}};
    func_ptr *p;

    // runs all the fcns
    for (p = &table[0][0]; p < &table[0][0] + 4; p++) {
        (*p)();
    }

    // calls 0th fcn
    p = &table[0][0];
    (*p)(); p++;;
    (*p)();

    // calls 2nd fcn
    table[0][1]();

    return 0;
}

现在我想要的是将参数传递给函数 - 例如,更改void Phase(void)

void Phase(int mode) 并通过一些类似的构造调用它:

table[1][1](TRUE);

但我还没有弄清楚如何做到这一点。欢迎任何帮助。

4

0 回答 0