c - 当我传入函数指针时出现错误

标签 c function pointers

我需要传入一个函数作为参数。我见过很多网站使用这种格式,他们说无论在函数名称之前有或没有 & 都可以使用这种格式,无论哪种方式,它都会给我一个错误。

static void iterate_test(void *node);
void delete(void *item);
static void printing(FILE *fp, const char *key, void *item);

int
main()
{   
    char *name = "name.txt";
    int *intp = malloc(sizeof(int));
    typedef void (*func1)(void*, char*, void*) = iterate_test;
    FILE *fp = fopen(name, "w");
    set_t *set = set_new(); // testing making a new set

    //Testing Iterate   
        //Function exists 
            set_iterate(set, intp, iterate_test);
        //Function doesn't exists 
            set_iterate(set, intp, iterate_test);
    //Testing Print

        //Printing normally
            set_print(set, fp, printing);
        //Printing with an inexistant function
            set_print(set, fp, printingfffff);
    //Testing Delete

        //Deleting a normal node
            set_delete(set, delete);
        //Deleteing from null set 
            set_delete(NULL, delete);
    fclose(fp);
    free(name);
    free(intp);
    return 0;
}

// Tests to see if it is able to move through the list set
static void iterate_test(void *node, char *key, void *item)
{
    if (node->key != NULL && node->item != NULL){
    }else{
    }
}

void delete(void *item)
{
    free(item);
}


/* Prints a single item */ 
static void printing(FILE *fp, const char *key, void *item)
{
  char *items = (char*)item;
  fputc('(', fp);
  fputs(key, fp);
  fputc(',', fp);
  fputs(items, fp);
  fputc(')', fp);
  fputc(',', fp);
  free(items);

即使我传入 func1 指针,它也会给我一个错误

我得到的错误/警告: 警告:从不兼容的指针类型传递“set_iterate”的参数 3

set_iterate 原型(prototype): 无效 set_iterate(set_t *set, 无效 *arg, void (*itemfunc)(void *arg, const char *key, void *item) );

最佳答案

 typedef void (*func1)(void*, char*, void*) = iterate_test;

是非法的,因为typedef定义了类型,而不是变量

<小时/>

声明

 static void iterate_test(void *node);

错误,因为函数是

 static void iterate_test(void *node, char *key, void *item)
 {
    ...
 }

将其替换为

 static void iterate_test(void *node, char *key, void *item);
<小时/>

Even when I pass in the func1 pointer it gives me an error

经过之前的更正后,void (*func1)(void*, char*, void*) = iterate_test; 是正确的(没有typedef)

<小时/>

error/warning I get: warning: passing argument 3 of 'set_iterate' from incompatible pointer type

set_iterate prototype: void set_iterate(set_t *set, void *arg, void (*itemfunc)(void *arg, const char *key, void *item) );

set_iterate 所期望的函数指针需要一个 const char * 作为其第二个参数,但 iterate_test 有一个 char * 为其第二个参数,这就是您收到该消息的原因

删除set_iterate配置文件中的const或将iterate_test的声明和定义修改为

 static void iterate_test(void *node, const char *key, void *item);
 ...
 static void iterate_test(void *node, const char *key, void *item)
 {
     ...
 }

当然,在第二种情况下,如果您仍然想将指针保存在func1中,您需要拥有

void (*func1)(void*, const char*, void*) = iterate_test;

关于c - 当我传入函数指针时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55770734/

相关文章:

c - 消除无序数组中的重复元素

c - gtk_window_set_default_size 不起作用

c - 错误: return value type does not match the function type

c - calloc 的两个参数

c - 将无符号字符数组作为参数传递 - 数组未正确传递

C++ 如何创建一个具有可选参数的函数

c - 从函数传递一 block 内存

c - 输出不匹配

c - 在循环中 fork 会导致奇怪的行为

c++ - 将 1 到 100 之间的数字相加 OpenMP