简体   繁体   English

C-如何设置函数指针的关联数组?

[英]C - How to setup an associative array of function pointers?

Here's what I'm trying to do: 这是我想做的事情:

myValueType function1(myParam){}

myValueType function2(myParam){}

myArray[CONSTANT_STATE1] = &function1;
myArray[CONSTANT_STATE2] = &function2;

myValue = (*myArray[CONSTANT_STATE1])(myParam);

When I compile, it throws an error that I've redeclared function1. 当我编译时,它会引发一个错误,我已经重新声明了function1。

What's the best way to do this? 最好的方法是什么?

As per this SO answer from user Vijay Mathew : 根据用户Vijay Mathew的 回答

Section 6.6 of The C Programming Language presents a simple dictionary (hashtable) data structure. C编程语言的 6.6节介绍了一个简单的字典(哈希表)数据结构。 I don't think a useful dictionary implementation could get any simpler than this. 我不认为有用的字典实现会比这更简单。 For your convenience, I reproduce the code here. 为了您的方便,我在这里重现代码。

struct nlist { /* table entry: */
    struct nlist *next; /* next entry in chain */
    char *name; /* defined name */
    char *defn; /* replacement text */
};

#define HASHSIZE 101
static struct nlist *hashtab[HASHSIZE]; /* pointer table */

/* hash: form hash value for string s */
unsigned hash(char *s)
{
    unsigned hashval;
    for (hashval = 0; *s != ’\0’; s++)
      hashval = *s + 31 * hashval;
    return hashval % HASHSIZE;
}

/* lookup: look for s in hashtab */
struct nlist *lookup(char *s)
{
    struct nlist *np;
    for (np = hashtab[hash(s)]; np != NULL; np = np->next)
        if (strcmp(s, np->name) == 0)
          return np; /* found */
    return NULL; /* not found */
}

char *strdup(char *);
/* install: put (name, defn) in hashtab */
struct nlist *install(char *name, char *defn)
{
    struct nlist *np;
    unsigned hashval;
    if ((np = lookup(name)) == NULL) { /* not found */
        np = (struct nlist *) malloc(sizeof(*np));
        if (np == NULL || (np->name = strdup(name)) == NULL)
          return NULL;
        hashval = hash(name);
        np->next = hashtab[hashval];
        hashtab[hashval] = np;
    } else /* already there */
        free((void *) np->defn); /*free previous defn */
    if ((np->defn = strdup(defn)) == NULL)
       return NULL;
    return np;
}

char *strdup(char *s) /* make a duplicate of s */
{
    char *p;
    p = (char *) malloc(strlen(s)+1); /* +1 for ’\0’ */
    if (p != NULL)
       strcpy(p, s);
    return p;
}

Note that if the hashes of two strings collide, it may lead to an O(n) lookup time. 请注意,如果两个字符串的哈希冲突,则可能导致O(n)查找时间。 You can reduce the likely hood of collisions by increasing the value of HASHSIZE . 您可以通过增加HASHSIZE的值来减少可能发生的碰撞。 For a complete discussion of the data structure, please consult the book. 有关数据结构的完整讨论,请查阅本书。

The code you've shown is almost right. 您显示的代码几乎正确。 The problem is in your function declarations: 问题出在您的函数声明中:

myValueType function1(myParam){}

myValueType function2(myParam){}

These are old-style K&R non-prototyped declarations - the name of the parameter is myParam , and the type has not been specified. 这些是旧式的K&R非原型声明-参数名称为myParam ,并且尚未指定类型。 Perhaps you meant this? 也许你是这个意思?

myValueType function1(myParamType myParam){}

myValueType function2(myParamType myParam){}

Expanding your code out to a minimal compilable example: 将您的代码扩展到一个最小的可编译示例:

typedef int myValueType, myParamType;
enum { CONSTANT_STATE1, CONSTANT_STATE2 };

myValueType function1(myParamType myParam){}

myValueType function2(myParamType myParam){}

void f(myParamType myParam)
{
    myValueType myValue;
    myValueType (*myArray[2])(myParamType);

    myArray[CONSTANT_STATE1] = &function1;
    myArray[CONSTANT_STATE2] = &function2;

    myValue = (*myArray[CONSTANT_STATE1])(myParam);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM