c:空隙大小* [英] c: size of void*
问题描述
我对 C 中的 void* 指针有点困惑.尤其是在阅读了这个问题之后:sizeof(some pointer) 总是等于四吗?,其中有人说不能保证 sizeof(int *) == sizeof(double *)
I'm a bit confused with a void* pointer in C. Especially after reading this question: Is the sizeof(some pointer) always equal to four?, where one person says there is no guarantee that sizeof(int *) == sizeof(double *)
我的问题是:是否有 sizeof(void*) >= sizeof(任何其他指针类型)的保证?换句话说,我可以总是将 some_type* 指针分配给 void* 指针,然后将其作为 some_type* 取回吗?
My question is: is there a guarantee of sizeof(void*) >= sizeof(any other pointer type)? In other words, can I always assign a some_type* pointer to a void* pointer and then get it back as some_type*?
推荐答案
仅数据指针.void *
可以存放任何数据指针,但不能存放函数指针.
Only data pointers. void *
can hold any data pointer, but not function pointers.
这是 C 常见问题解答.
void *'s 只保证保存对象(即数据)指针;它将函数指针转换为 void * 类型是不可移植的.(一些机器,函数地址可以非常大,比任何数据都大指针.)
void *'s are only guaranteed to hold object (i.e. data) pointers; it is not portable to convert a function pointer to type void *. (On some machines, function addresses can be very large, bigger than any data pointers.)
至于第一部分,是的,不同类型可以有不同大小的指针:
As for the first part, yes, different types can have pointers of different sizes:
这篇关于c:空隙大小*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!