Address in int

J

john

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);

?
 
R

Richard Heathfield

john said:
Is it possible to convert a pointer value to an int?
like this:
int *ptr = &blah;
int address = (int)ptr;

Yes, that's legal, although there is no guarantee that you will not lose
information during the conversion.
And is is legal to do:

printf("%d %d", ptr, &blah);

No, but you can do:

printf("%p %p\n", (void *)ptr, (void *)&blah);
 
A

Alexander Bartolich

john said:
Is it possible to convert a pointer value to an int?
like this:
int *ptr = &blah;
int address = (int)ptr;

Sure.
On some systems this conversion will truncate the value, though.
And the way back is not at all guaranteed to work.

int* ptr = (int*)address;
And is is legal to do:

printf("%d %d", ptr, &blah);

No. You need an explicit conversion.
Image a system where int are two bytes and pointers four.

printf("%d %d", ptr, (int)&blah);

In this special case %p might be better, anyway.
 
T

Tomás

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.

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).

-Tomás
 
K

Keith Thompson

Alexander Bartolich said:
john wrote: [...]
And is is legal to do:

printf("%d %d", ptr, &blah);

No. You need an explicit conversion.
Image a system where int are two bytes and pointers four.

printf("%d %d", ptr, (int)&blah);

In this special case %p might be better, anyway.

You need to cast both arguments:

printf("%d %d", (int)ptr, (int)&blah);

Though, as others have already pointed out, it makes little sense to
do this. Use "%p" to print pointers:

printf("%p %p", (void*)ptr, (void*)&blah);
 
K

Keith Thompson

Tomás said:
john posted:


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".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top