c为变量分配外部函数函数

时间:2014-11-28 12:21:48

标签: c functional-programming function-pointers

我有一个结构,其中包含一个函数变量,我想为其分配一个函数,它本身在全局级别上可见,如下所示:

typedef struct HashMap{
    struct LinkedList** datapointers;
    int key_space;
    int* (*hash_function)(const char*);
}HashMap;
unsigned int hashfunction(const char* input){
    int i=0;
    int hash=0;
    while(input[i]){
        hash=hash+input[i]; 
        i++;
    }
    return hash;
}
//create a new hashmap 
HashMap* create_hashmap(int key_space){
    HashMap *hm = malloc (sizeof(HashMap));
    hm->datapointers = malloc(sizeof(LinkedList)*key_space); //array of linked lists 
    hm->key_space=key_space; 
    for(int i=0;i<key_space;i++){ //initalize to NULL 
        hm->datapointers[i]=NULL;
    }
    hm->hash_function=*hashfunction;
    return hm;
}

现在无论我做什么,我都会不断收到无效指针或未声明的变量。有没有办法让这个工作正常?

谢谢

4 个答案:

答案 0 :(得分:1)

你应该编码

  hm->hash_function = hashfunction;

另外,您的hashfunction签名不正确。将其定义为:

  int hashfunction(const char* input)

没有 unsigned,并声明字段:

 int (*hash_function)(const char*);
顺便说一句,你更了解closures是什么。您可能应该考虑向每个使用间接的函数添加一些客户端数据。例如,您可以在void* client_data;内添加字段struct HashMap。另见this。详细了解callbacks

最后,我发现使用typedef签名(用于指向函数的指针)声明here更具可读性。

答案 1 :(得分:1)

您的代码应该是

hm->hash_function = hashfunction;

但是,存在不匹配。

函数指针类型为int* (*hash_function)(const char*);

但你的功能签名是

unsigned int hashfunction(const char* input)

IMO,应该是

  1. int* hashfunction(const char* input) [需要相应修改功能定义]
    1. unsigned int (*hash_function)(const char*);

答案 2 :(得分:1)

我收到了一堆警告。这是修改后的代码,没有给出任何警告:

typedef struct HashMap{
    struct LinkedList** datapointers;
    int key_space;
    int (*hash_function)(const char*);
}HashMap;
int hashfunction(const char* input){
    int i=0;
    int hash=0;
    while(input[i]){
        hash=hash+input[i]; 
        i++;
    }
    return hash;
}
//create a new hashmap 
HashMap* create_hashmap(int key_space){
    HashMap *hm = malloc (sizeof(HashMap));
    hm->datapointers = malloc(sizeof(LinkedList)*key_space); //array of linked lists 
    hm->key_space=key_space; 
    for(int i=0;i<key_space;i++){ //initalize to NULL 
        hm->datapointers[i]=NULL;
    }
    hm->hash_function=hashfunction;
    return hm;
}

变化是: 1.函数指针hash_function不返回int *但返回int。

  1. 哈希函数现在返回int而不是unsigned int。

  2. 在为hash_function分配哈希函数时,请不要取消引用它。

答案 3 :(得分:0)

The following is the correct code.
However, since the LinkedList is not defined in your posted code, 
it fails to compile.


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

// where/what is the definition of LinkedList?

// define the HashMap struct
struct HashMap
{
    struct LinkedList** datapointers;
    int key_space;
    unsigned int (*hash_function)(const char*);
};

// prototypes
unsigned int hashfunction(const char*);
struct HashMap* create_hashmap(int);


unsigned int hashfunction(const char* input)
{
    int i=0;
    unsigned int hash=0;

    while(input[i])
    {
        hash += input[i];
        i++;
    } // end while

    return hash;
} // end function: hashfunction


//create a new hashmap
struct HashMap* create_hashmap(int key_space)
{
    // define an instance of the HashMap struct
    struct HashMap *hm = malloc (sizeof(struct HashMap));
    if( NULL == hm )
    { // then malloc failed
        perror( "malloc of HashMap failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, malloc successful

    // array of linked lists
    if( NULL == hm->datapointers = malloc(sizeof(LinkedList)*key_space) )
    { // then, malloc failed
        perror( "malloc of HashMap.datapointer failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, malloc successful

    // fill in fields of HashMap
    hm->key_space=key_space;
    memset( hm->datapointers, 0x00, sizeof(LinkedList*key_space) );
    hm->hash_function = &hashfunction;

    return hm;
} // end function: create_hashmap