Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Address in int

Reply
Thread Tools

Address in int

 
 
john
Guest
Posts: n/a
 
      05-13-2006
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);

?
 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      05-13-2006
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);


--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
 
 
 
Alexander Bartolich
Guest
Posts: n/a
 
      05-13-2006
john wrote:
> 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.

--
Ich kenne die Mißverständnisse-FAQ, und sie wird oft mißverstanden.
-- Andreas M. Kirchwitz <(E-Mail Removed)>
 
Reply With Quote
 
Tomás
Guest
Posts: n/a
 
      05-13-2006
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

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-13-2006
Alexander Bartolich <(E-Mail Removed)> writes:
> 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);

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-13-2006
"Tomás" <(E-Mail Removed)> 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) (E-Mail Removed) <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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why is int a[0] not allowed, but int* a = new int[0] is? haijin.biz@gmail.com C++ 9 04-17-2007 09:01 AM
Difference between int i, j; and int i; int j; arun C Programming 8 07-31-2006 05:11 AM
int a[10]; int* p=(int*)((&a)+1); But why p isn't equal to ((&a)+1)? aling C++ 8 10-20-2005 02:42 PM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments