在我的main函数中调用函数指针

时间:2015-02-20 00:02:01

标签: c list struct hashtable function-pointers

所以我目前正在尝试测试一个我编程的函数指针,并且想知道在main中调用它的正确方法是什么?

我得到的当前错误是: “警告:不兼容的整数到指针转换传递'unsigned long'到       'unsigned long(*)(char *)'[-Wint-conversion]“

类型的参数

我认为我正在尝试调用此函数的方式。谢谢!

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

typedef struct sll sll;
struct sll {
  char *s;
  sll *next;
};

struct hash_table {
  unsigned long int(*hash)(char *);
  unsigned int n_buckets;
  sll **buckets; /* an array of pointers to string lists */
};
typedef struct hash_table htbl;

unsigned long int good_hash(char *s) {
  unsigned long int init = 17;
  int i; // count                                                                              
  for (i = 0; i <= strlen(s) - 1; i++) {
    init = 37 * init + s[i];
   }
  return init;
}


htbl *ht_new(unsigned long int(*h)(char*), unsigned int sz) {
  htbl *nh;
  nh = malloc(sizeof(htbl));
  nh->hash = h;
  nh->buckets = malloc(sizeof(sll)*sz);
  return nh;
}



int main() {
unsigned long int(*functionPtr)(char*) = &good_hash;
char *fork = "FORK";
 printf("%s\n", ht_new((*functionPtr)(fork), 30)->buckets[16]->s);
}

1 个答案:

答案 0 :(得分:0)

ht_new((*functionPtr)(fork), 30)

调用函数functionPtr指向,传递fork,然后将结果传递给ht_new

您想要的只是将functionPtr传递给ht_new

ht_new(functionPtr, 30)