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

Calling a function pointer in my main function

所以我目前正在尝试测试我编写的接受函数指针的函数,并且想知道在 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);
}
ht_new((*functionPtr)(fork), 30)

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

你想要的只是将 functionPtr 传递给 ht_new:

ht_new(functionPtr, 30)