🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.unix.programmer
5 messages
5 total messages Started by Borg Sat, 16 Nov 1996 00:00
Displaying addresses of variables in hex?
#3442
Author: Borg
Date: Sat, 16 Nov 1996 00:00
26 lines
1165 bytes
Hi,

A quick (dumb?) question:

How do I display variable addresses in hex rather than in decimal?
An example:

  double var;
  cout << "var value is " << var << " and its address is ";
  cout << unsigned (&var) << endl;

A sample output:

[vlad@borg pointers]$ address
var value is 6 and its address is 3221223436.

My (platform/OS independent) books insist on using:

cout << &var << endl; (without "unsigned" and without 
round brackets!) I am using gcc/g++ 2.7.2 on Linux-i586.

Please reply in newsgroups.

Thank you in advance,
Vlad
Re: Displaying addresses of variables in hex?
#3443
Author: Dietmar Kuehl
Date: Sun, 17 Nov 1996 00:00
32 lines
1777 bytes
Hi,
Borg (vladimip@iceonline.com*) wrote:
: How do I display variable addresses in hex rather than in decimal?
: An example:

:   double var;
:   cout << "var value is " << var << " and its address is ";
:   cout << unsigned (&var) << endl;

I'm not sure whether it is standard conforming to do a conversion from
some pointer to an 'unsigned' (IMO it should not) but given that it
works, you can use the 'hex' manipulator, i.e.

    cout << hex << unsigned (&var) << endl;

However, the correct way to print a pointer is like this:

    cout << &var << endl;

: My (platform/OS independent) books insist on using:

: cout << &var << endl; (without "unsigned" and without 
: round brackets!) I am using gcc/g++ 2.7.2 on Linux-i586.

And, guess what, they are correct! Every pointer is convertible to
'void const *' and there is an inserter for exactly this type which
prints the address in an implementation defined way, normally in hex.
--
<mailto:dietmar.kuehl@uni-konstanz.de>
<http://www.informatik.uni-konstanz.de/~kuehl/>
I am a realistic optimist - that's why I appear to be slightly pessimistic
Re: Displaying addresses of variables in hex?
#3444
Author: Rohan Lenard
Date: Sun, 17 Nov 1996 00:00
19 lines
1175 bytes
In article <56m5ch$noi@news.iceonline.com>, Borg <vladimip@iceonline.com*> writes:
:How do I display variable addresses in hex rather than in decimal?
:An example:
:
:  double var;
:  cout << "var value is " << var << " and its address is ";
:  cout << unsigned (&var) << endl;

Try -
   cout << (void*)&var << endl;


Rohan
-- 
Rohan Lenard - rjl@wr.com.au rjl@ot.com.au rjl@triode.net.au
+61-4-11250024		| All quotes can be attributed to my automated quote
+61-2-99649633		| writing tool.  Yours for just $19.95; and if you
<gnu service provider>	| call now you'll get a free set of steak knives ...
Re: Displaying addresses of variables in hex?
#3445
Author: Oleg Krivosheev
Date: Sun, 17 Nov 1996 00:00
37 lines
1310 bytes

Hi,

Borg <vladimip@iceonline.com*> writes:

> 
> Hi,
> 
> A quick (dumb?) question:
> 
> How do I display variable addresses in hex rather than in decimal?
> An example:
> 
>   double var;
>   cout << "var value is " << var << " and its address is ";
>   cout << unsigned (&var) << endl;
> 
> A sample output:
> 
> [vlad@borg pointers]$ address
> var value is 6 and its address is 3221223436.
> 
> My (platform/OS independent) books insist on using:
> 
> cout << &var << endl; (without "unsigned" and without 
> round brackets!) I am using gcc/g++ 2.7.2 on Linux-i586.
> 
> Please reply in newsgroups.

i don't know is this bug or not, but in order to
get address printed, you have to explicitely cast it to
(void*).

cheers

OK
Re: Displaying addresses of variables in hex?
#3441
Author: Ronald Fischer
Date: Mon, 18 Nov 1996 00:00
20 lines
1230 bytes
Borg wrote:
> 
> Hi,
> 
> A quick (dumb?) question:
> 
> How do I display variable addresses in hex rather than in decimal?

To output the address, cast the pointer to void* (because some other pointer
types, such as char*, get special treatment). To get the output in hex,
use the hex inserter:
	ostr << hex << (void const*)&variable;

-- 

Ronald Otto Valentin Fischer
business: ronald.fischer@uebemc.siemens.de
private:  ronald.fischer@acm.org
http://ourworld.compuserve.com/homepages/ronald_fischer
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads