[英]Get the hexadecimal address of a variable as uintptr_t
int y = 1;
int *x = &y;
printf("%p\n",x); // instead of printing, get this into variable of type unintptr_t
I would like to get the address x
into variable of type uintptr_t
我想将地址
x
转换为uintptr_t
变量
Is there a way to do that in C? 有没有办法在C中做到这一点?
It isn't particularly difficult... 并不是特别困难...
uintptr_t z = (uintptr_t)x;
Notice that the result of this cast is implementation-defined; 注意,这个强制转换的结果是实现定义的; the only guarantee you have is that if you cast
z
back to an int *
you'll get the original pointer back. 唯一的保证是,如果将
z
强制转换为int *
您将获得原始指针。
By the way, there's no such a thing as a "hexadecimal address"; 顺便说一句,没有“十六进制地址”这样的东西。 addresses are addresses, they can be seen as numbers in whatever base you like most, showing them in hexadecimal base is just a convention (which has some advantages).
地址是地址,可以将它们看作是您最喜欢的任意基数的数字,以十六进制基数显示它们只是一个约定(这有一些优点)。
您可以通过将x的地址显式转换为uintptr_t来存储值。
uintptr_t z = (uintptr_t)&x;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.