有没有推荐的整型来存储函数指针在标准C [英] Is there a recommended integer type to store function pointers in standard C

查看:187
本文介绍了有没有推荐的整型来存储函数指针在标准C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C99标准有 uintptr_t形式,推荐的整数类型数据的指针(对象指针)转换为,但我没有找到一个相应的整数类型来存储函数指针。难道我忽略了吗?

The C99 standard has uintptr_t, a recommended integer type to convert data pointers (pointers to objects) to, but I did not find an equivalent integer type to store function pointers. Did I overlook it?

一个特定的编译器可以定义这种类型的,即使它是不是在标准,但是一个编译器更可能指出一个函数指针可以被存储在(比方说)一个 uint64_t中,而不是定义一个新类型。

A specific compiler could define such a type even though it is not in the standard, but a compiler is more likely to state that a function pointer can be stored in (say) a uint64_t than to define a new type.

的另一个区别是,它可以使感上做的方式的数据指示字,它不会对函数指针整数运算。一个常见的​​成语是(INT *)(((uintptr_t形式)P + 15)及〜(uintptr_t形式)15),但没有理由这个成语适用于功能指针。

Another difference is that it can make sense to do integer arithmetic on a data pointer in ways that it doesn't on function pointer. One common idiom is (int*)(((uintptr_t)p + 15) & ~(uintptr_t)15) but there is no reason to apply this idiom to a function pointer.

推荐答案

没有有没有这样的推荐指针类型。

No there is no such recommended pointer type.

要的商店的你可以使用任何其它的函数指针类型,只要你不通过它调用你的函数指针。

To store the pointer you may use any other function pointer type, as long as you don't call your function through it.

如果您转换您的指针不是回到原来​​的类型可能会调用你的函数。这是始终保证工作。

If you convert your pointer than back to the original type you may call your function. This is always guaranteed to work.

这篇关于有没有推荐的整型来存储函数指针在标准C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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