473,388 Members | 1,526 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Address in int

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

?
May 13 '06 #1
5 5018
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)
May 13 '06 #2
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 <sl****************@krell.zikzak.de>
May 13 '06 #3
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

May 13 '06 #4
Alexander Bartolich <al*****************@gmx.at> 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) 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.
May 13 '06 #5
"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.
May 13 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Alexander N. Spitzer | last post by:
If I have a machine with 3 virtual IP addresses (192.168.1.), how can I start 3 instances of the same RMI application (each started with different properties/configs), each listening on the port...
8
by: YAN | last post by:
Hi, I want to get the mac address from a machine, which i have the IP address of that machine, how can i do that? I know how to get the mac address of the local machine from the following code: ...
7
by: Privacy Advocate | last post by:
//crossposted to: comp.lang.javascript, alt.comp.lang.javascript in an effort to get factual answers from JavaScript experts// Simply put; Is it possible to obtain the real (actual) IP address of...
33
by: baumann.Pan | last post by:
hi all, i want to get the address of buf, which defined as char buf = "abcde"; so can call strsep(address of buf, pointer to token);
4
by: andreas.w.h.k. :-\) | last post by:
How do I change the address location in the wsdl <wsdl:port name="SearchSoap12" binding="tns:SearchSoap12"> <soap12:address location="http://searchservices/engine/search.asmx" /> </wsdl:port> ...
1
by: Phoenix_ver10 | last post by:
I have a mailing list with multiple names going to the same addresses. I need one address with all the names for that address on it. I checked out the example on microsoft's site, but A: It doesn't...
1
by: Jamie J. Begin | last post by:
I'm very new to the world of Python and am trying to wrap my head around it's OOP model. Much of my OOP experience comes from VB.Net, which is very different. Let's say I wanted to create an...
6
by: Nicolas Noakes | last post by:
Hello, I would like to convert to following process to code. Any advice is welcome. I have a hardware device which requires the this procedure to set it's IP address. First create an static...
36
by: Julienne Walker | last post by:
Ignoring implementation details and strictly following the C99 standard in terms of semantics, is there anything fundamentally flawed with describing the use of a (non-inline) function as an...
1
by: saravanatmm | last post by:
I need javascript code for validate the email address. Email address field cannot allowed the capital letters, special characters except '@' symbol. But can allowed the small letters, numeric...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.