C:什么是函数指针转换? [英] C: What even is function pointer conversion?

查看:86
本文介绍了C:什么是函数指针转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设某人想要创建一个可以容纳多个函数指针的数组& ..他会怎么做呢?也许无效指针数组可以工作吗?...事实证明,没有,因为要使用存储在无效指针中的函数,您必须将它们(无效指针)转换/投射回函数指针,然后...

Let's say one wanted to create an array that could hold multiple function pointers & of different types.. how would he go about doing so ? Perhaps an array of void pointers could work ?... Well as it turns out, no since in order to use the functions stored in the void pointers you'd have to convert/cast them (the void pointers) back to function pointers and...

在函数指针和无效指针之间的转换是不可能的. void *从来不是函数指针的泛型指针类型,仅是对象指针.它们是不兼容的类型.

"A conversion between a function pointer and a void pointer is not possible. void* was never a generic pointer type for function pointers, only for object pointers. They are not compatible types.

[...]您可以在void *和函数指针之间进行强制转换,但是会发生未定义的行为.您将依赖于系统特定的非标准扩展."

[...] you can cast between void* and a function pointer, but what will happen is undefined behavior. You will be relying on system-specific non-standard extensions."

-@ Lundin(此处)

-@Lundin (here)

相反...

稍微好一点的方法是使用诸如void(*)(void)之类的函数指针类型作为通用指针.您可以转换为不同的函数指针或从不同的函数指针转换为,这将是编译器特定的(实现定义的行为).也就是说,代码仍将是不可移植的,但是至少您不会因调用未定义的行为而冒着程序崩溃的风险.

Slightly better would be to use a function pointer type such as void (*)(void) as the generic pointer. You can convert to/from different function pointers, what will happen is compiler-specific (implementation-defined behavior). That is, the code will still be non-portable, but at least you don't risk a program crash from invoking undefined behavior.

-@ Lundin(此处)

-@Lundin (here)


也就是说,从一个函数指针转换为另一个函数指针是什么意思?


That said, what does it even mean to convert from one function pointer to another ?

有人可以解释.也许有一个例子.

Could someone explain it. With an example perhaps.

推荐答案

这意味着如果您具有两种不同的函数指针类型,例如:

It means that if you have two different function pointer types, such as for example:

int (*i)(int);
void (*v)(void);

然后,您可以使用显式强制转换将一种类型转换为另一种类型:

Then you can make a conversion from one type to the other by using an explicit cast:

v = (void(*)(void))i;

这是允许的,但调用v()无效.该标准说明了这一点,C17 6.3.2.3§8:

This is allowed, but calling v() won't work. The standard says this, C17 6.3.2.3 §8:

指向一种类型的函数的指针可以转换为指向另一种类型的函数的指针,并且 再次回来;结果应等于原始指针.如果将转换后的指针用于 调用其类型与引用的类型不兼容的函数,其行为是不确定的.

A pointer to a function of one type may be converted to a pointer to a function of another type and back again; the result shall compare equal to the original pointer. If a converted pointer is used to call a function whose type is not compatible with the referenced type, the behavior is undefined.

因此,如果您尝试通过v();调用该函数,则可能是未定义的行为.但是,您可以转换回原始类型int(*)(int)而不会丢失信息.

So if you try to call the function by v(); then it is likely undefined behavior. You can however convert back to the original type int(*)(int) without losing information.

这允许您将特定的函数指针类型用作通用类型". .而不是使用void*,这是不可行的,如此处所述.

This allows you to use a specific function pointer type as a "generic type". Rather than using void*, which isn't ok, as discussed here.

值得注意的是,如果使用typedef,所有带有函数指针的代码都将更易于阅读:

Notably, all code with function pointers get much easier to read if you use typedefs:

typedef int int_func (int);
typedef void void_func (void);

int main() 
{
  int_func*  i;
  void_func* v;

  v = (void_func*) i;

  i = (int_func*) v;
}

这篇关于C:什么是函数指针转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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