可以无效*用于存储函数指针? [英] can void* be used to store function pointers?
问题描述
无效*以这样的方式,它可以指向任何东西所定义。所以可以把它用于指向一个函数(INT的send())?
void* is defined in such a way that it could point any thing. So can it be used to point a function (int send())?
int send();
void* p = send;
这可能吗?当我这样使用它没有显示我的错误,为什么?如果没有,有没有什么办法来存储所有的指针在一个变量?
Is it possible? When i use like this it is not showing me errors why? If not, Is there any way to store all pointers in a single variable?
推荐答案
没有也可能不是。
根据C标准(6.3.2.3指针)
According to the C Standard (6.3.2.3 Pointers)
1至无效的指针可被转换成或从一个指针到任何
对象类型即可。一个指向任何对象类型可以被转换为指针失效,然后再返回;结果应比较等于
原来的指针。
1 A pointer to void may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.
对于函数指针再
8的指针一种类型的功能可以被转换为一个指针
另一种类型的,然后再返回的函数;结果应该比较
等于原始指针。如果转换后的指针可以用来调用
一个函数,其类型不与引用的类型兼容,
行为是不确定的。
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.
在C ++标准有指针的更详细的定义(3.9.2混合类型)
In the C++ Standard there is more detailed definition of pointers (3.9.2 Compound types)
3键入指针为void 或一个指向对象类型
被称为对象指针类型 ....类型的指针,可以
指定一个函数叫做函数指针类型
3 The type of a pointer to void or a pointer to an object type is called an object pointer type....The type of a pointer that can designate a function is called a function pointer type.
和
4 指向cv合格(3.9.3)或CV-不合格无效可以使用
指向0 bjects未知类型即可。这样的指针应能
持有任何对象的指针。类型品种的void *的对象应具备
同样重presentation和对齐要求CV的char *。
4 A pointer to cv-qualified (3.9.3) or cv-unqualified void can be used to point to objects of unknown type. Such a pointer shall be able to hold any object pointer. An object of type cv void* shall have the same representation and alignment requirements as cv char*.
这篇关于可以无效*用于存储函数指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!