我试图将 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/