c - 将 void * 指针变量从 unsigned int 类型转换为 uint32 时出现编译时错误

标签 c pointers casting

我试图将 void * 类型的值转换为 uint32 但编译后出现错误:

error: cast from pointer to integer of different size

最佳答案

C 标准所说的是这样的,C11 6.3.2.3/6:

Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.

这意味着由编译器来指定给定系统上将发生什么。显然,您的特定编译器认为结果无法表示,因此会抛出诊断消息。

您应该做的是转换为uintptr_t。该类型保证足够大以包含对象指针值。

关于c - 将 void * 指针变量从 unsigned int 类型转换为 uint32 时出现编译时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50675039/

相关文章:

c - 如何将 uint8_t 值数组转换为 C 中的十六进制转储 char 字符串

C - 使用 Controller 函数的 Pthreads

c - 修复签名和未签名警告之间比较的解决方案

c - 在 C 中将整数值存储为字符串

c++ - 指向函数参数与函数参数的指针?

Java 转换接口(interface)但使用对象方法

c - 矩阵乘法的动态内存分配

c++ - 从 C++ 中的函数返回数组

c++ - 在遗留代码上规避 RTTI

scala 反射,使用 asInstanceOf 进行类型转换