void 指针可以安全地指向另一个指针吗?

标签 c pointers void

这可能是显而易见的,但是一个 void 指针可以安全地指向另一个指针吗?即指向一个类型 int *

int i = 5;
int *ip = &i;
void *vp = &ip;

int *nip = *(int **)vp;
int ni = *nip; // == 5?

编辑:抱歉,我可能没说清楚,我想知道 void 指针是否可以指向具有另一个指针类型的内存部分;而不是一个空**。只是无效 *

最佳答案

是的,您可以将任何对象指针转换为 void * 并返回到原始指针类型,而不会丢失信息。

出自马口:

(C99, 6.3.2.3p1) "A pointer to void may be converted to or from a pointer to any incomplete or object type. A pointer to any incomplete or object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer."

关于void 指针可以安全地指向另一个指针吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19329437/

相关文章:

c - 释放时出现三重指针错误

c++ - 对指向 const 对象的非常量指针的非常量引用

c - pthread 空指针转换

c++ - 如何检查 Void 指针是否可以转换为其他特定指针类型?

c - 将 sizeof 与 sprintf/vsnprintf 一起使用会损坏文本

c++ - Eclipse:缺少 C++ 运行配置?

c - 使用 VS Code、Makefile 和自定义 bash 脚本在 C 语言中调试头文件

无法访问结构成员

c - 了解程序集中的条件代码标志设置

c++ - sizeof(void()) 是合法的表达式吗?