C 获取变量的十六进制地址为uintptr\u t
我想将地址C 获取变量的十六进制地址为uintptr\u t,c,pointers,casting,type-conversion,C,Pointers,Casting,Type Conversion,我想将地址x放入uintpttr\u t 在C语言中有什么方法可以做到这一点吗?这并不特别困难 int y = 1; int *x = &y; printf("%p\n",x); // instead of printing, get this into variable of type unintptr_t 请注意,此强制转换的结果是实现定义的;唯一可以保证的是,如果您将z转换回int*,您将获得原始指针 顺便说一下,没有“十六进制地址”这样的东西;地址就是地址,它们可以被看作是
x
放入uintpttr\u t
在C语言中有什么方法可以做到这一点吗?这并不特别困难
int y = 1;
int *x = &y;
printf("%p\n",x); // instead of printing, get this into variable of type unintptr_t
请注意,此强制转换的结果是实现定义的;唯一可以保证的是,如果您将z
转换回int*
,您将获得原始指针
顺便说一下,没有“十六进制地址”这样的东西;地址就是地址,它们可以被看作是以你最喜欢的任何一种基址表示的数字,以十六进制基址表示它们只是一种惯例(它有一些优点) 您可以通过显式地将addAddress x类型转换为uintpttr\t来存储该值
uintpttr\u t z=(uintpttr\u t)&x代码>我缺少什么<代码>uintptr_t ptr=(uintptr_t)x代码>?如果不是重复的,则与相关。@John3136n(uintptr_t)x代码>缺少所需的强制转换void*
。详细信息:x
不是void*
或类似文件。从int*
转换到(uintpttr\t)
的定义不太明确。6.3.2.3建议uintpttr_t z=(uintpttr_t)(void*)x代码>C117.20.1.4 1。“如果你把z转换回int*你会得到原来的指针。”这几乎与规范一致,转换需要经过void*
,然后得到一个等价的指针,可能不是原来的指针。
uintptr_t z = (uintptr_t)x;