如何在c中将指针转换为整数?

how to convert pointer to integer in c?

解决此问题的最简单方法,请展示您的作品

尝试

int x = (int)p

错误

警告:从指针转换为不同大小的整数 [-Wpointer-to-int-cast]

使用整数类型保存指针值的最佳方法是使用 uintptr_t 类型。

来自 C11,章节 7.20.1.4,

The following type designates an unsigned integer type with the property that any valid pointer to void can be converted to this type, then converted back to pointer to void, and the result will compare equal to the original pointer:

 uintptr_t

如果您想要有符号整数类型,请使用 intptr_t