"Tomás" <NU**@NULL.NULL> writes:
john posted:
Is it possible to convert a pointer value to an int?
like this:
int *ptr = &blah;
int address = (int)ptr;
And is is legal to do:
printf("%d %d", ptr, &blah);
?
It's not portable -- but it will work on most systems.
I would have stopped after "It's not portable".
It will work on even more systems if you use an "unsigned long long":
typedef unsigned long long uLL;
int k;
uLL address = (uLL)&k;
(Off-Topic: The C++ Standard says that this will work perfectly, but only
if the integral type has enough bits to store all off the address's info).
It says that what will work perfectly?
I can believe that the C++ standard would guarantee that
pointer-to-integer-to-pointer conversion yields the original pointer
value as long as the integer type is big enough (however it defines
"big enough"). C makes such a guarantee only for intptr_t and
uintptr_t, which may or may not exist. But there's no reason to go
through these gyrations rather than simply using "%p".
--
Keith Thompson (The_Other_Keith)
ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.