函数指针不兼容的指针类型从void(my_type *)变为void(*)(void *) [英] function pointer incompatible pointer types void (*)(void *) from void (my_type *)

查看:282
本文介绍了函数指针不兼容的指针类型从void(my_type *)变为void(*)(void *)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经收到上述警告并理解了,但是只是不知道如何解决.我的代码在下面,但是基本上我正在做的是在一个结构体中存储一个函数指针,并在我从main调用的另一个函数中初始化该结构体.当我将其与默认函数(即free())一起使用时,代码似乎可以编译,而当我与以下test_func函数一起使用时,则代码无法编译.有想法吗?

I'm having getting the above warning and understand it, but just don't know how to fix it. My code is below, but basically what I'm doing is storing a function pointer in a struct, and initializing the struct in another function that I call from my main. The code seems to compile when I use it with a default function (i.e. free()) but not when I use it with the test_func function below. Thoughts?

结构:

  typedef struct my_struct {
  my_type **double_pointed;
  int num;
  void (*funcp)(void *data);
  } my_struct_t;

初始化功能:

my_struct_t *init(int num, void (*my_funcp)(void *data)) {

    // unimportant code to the problem, but basically just some mallocs for the other data

  my_struct_t *my_str;
  my_str->funcp = my_funcp;

  return struct;
}

我想指出的功能:

void desired_func(my_type *obj) {}

我对init函数的调用:

my call to the init func:

init(5, desired_func);

完全错误:

不兼容的指针类型从分配给'void(*)(void *)' '无效(my_type *)'

incompatible pointer types assigning to 'void (*)(void *)' from 'void (my_type *)'

上面的init函数中的行:

on the line in the init function above:

my_struct_t *my_str;
my_str->funcp = my_funcp;

推荐答案

更改函数指针类型以匹配函数,即. :

Change the function pointer types to match the function, ie. :

void (*funcp)(my_type *data);

如果您希望能够使用具有不同签名(例如,不同参数类型)的函数,则可以添加显式强制转换.例如:

If you want to be able to use functions with different signatures (eg. different parameter types), you can add explicit casts. For example :

init(5, (void (*)(void*)) desired_func);

请注意,每当需要通过函数指针调用desired_func函数(或其签名与funcp函数指针不匹配的任何其他函数)时,都必须将函数指针强制转换回其原始类型(在这种情况下为void (*)(my_type*)),然后调用该函数,否则它将调用未定义的行为.

Note that any time the desired_func function (or any other function whose signature doesn't match the funcp function pointer) needs to be called through the function pointer, the function pointer will have to be cast back to its original type (void (*)(my_type*) in this case) before calling the function, or it invokes undefined behavior.

另一种(更好一些)的替代方法是更改​​desired_func定义,如下所示:

Another (somewhat better) alternative is to change the desired_func definition like this :

void desired_func(void *vobj) {
    my_type* obj = vobj;
    /* rest of the code */
}

请注意,在调用desired_func(通过函数指针或直接通过函数指针)时,其地址作为参数传递的对象的类型必须为my_type,或在函数内部取消引用obj(如果对齐方式不同)甚至演员本身也将调用未定义的行为.

Note that when calling desired_func (through a function pointer or directly), the type of the object whose address is passed as parameter needs to be my_type, or dereferencing obj inside the function (and if alignment differs even the cast itself) will invoke undefined behavior.

处理铸造时潜在的不确定行为的可能性增加了复杂性(如上所述),这意味着我不建议铸造-需要此类铸造可能表明您的设计存在问题. IE.如果您再考虑一下自己的需求,可能会有更好的解决方案.

The added complexity for dealing with the potential for undefined behavior when casting (as noted above) means that I would advise against casting - needing such casts is probably indicative of a problem in your design. Ie. there's probably a better solution if you think about your requirements a bit more.

这篇关于函数指针不兼容的指针类型从void(my_type *)变为void(*)(void *)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆