displaying address of a character array ?

W

Wondering Wanderer

hi,

If I declare a character array ( say, char arr[10] ), how do I see the
address of the first element of the array ? simply using cout << arr or
cout << &arr displays the entire string in the array, not the address.

thanks.
 
P

Phlip

Wondering said:
If I declare a character array ( say, char arr[10] ), how do I see the
address of the first element of the array ? simply using cout << arr or
cout << &arr displays the entire string in the array, not the address.

That's because << overloads to operator<<(char *), which you don't need.

To get operator<<(unsigned long), you must typecast:

cout << "0x" << hex << reinterpret_cast<unsigned long>(arr);

I switched to hexadecimal to make the bit patterns in the pointer clearer.

Question for the crew; will this work?

typedef unsigned long ULong;
cout << "0x" << hex << ULong(arr);
 
V

Victor Bazarov

Wondering said:
If I declare a character array ( say, char arr[10] ), how do I see the
address of the first element of the array ? simply using cout << arr
or cout << &arr displays the entire string in the array, not the
address.

cout << static_cast<void*>(arr);

V
 
R

Rolf Magnus

Wondering said:
hi,

If I declare a character array ( say, char arr[10] ), how do I see the
address of the first element of the array ? simply using cout << arr or
cout << &arr displays the entire string in the array, not the address.

Try cout << static_cast<void*>(arr)
 
W

Wondering Wanderer

thanks victor, phlip and rolf. there's another typecasting way too :
cout << (int *) arr ;
 
W

werasm

Phlip said:
Question for the crew; will this work?

typedef unsigned long ULong;
cout << "0x" << hex << ULong(arr);

This conversion should require a reinterpret_cast. VC++7.1 compiles it
with warnings though if one specifies compiler option /Wp64. This means
that the cast may change the value on a 64bit system.

typedef unsigned long long ULong;//...

....seems to make this warning go away.

see c++ standard98:

5.2.10/3 - mapping of reinterpret_cast implementation defined
5.2.10/4 - A pointer can be explicitly (not implicitly) converted to
any integral type large enough to hold it.

Regards,

Werner
 
T

Tom Widmer

werasm said:
Phlip wrote:




This conversion should require a reinterpret_cast.

A C-style cast can do a reinterpret_cast. By 5.2.3, that code is
equivalent to:
cout << "0x" << hex << (ULong)arr;
which is equivalent to:
cout << "0x" << hex << reinterpret_cast<ULong>(arr);

Tom
 
W

werasm

Tom said:
A C-style cast can do a reinterpret_cast. By 5.2.3, that code is
equivalent to:

Yes, but that was not a c-style cast :). That was instantiating a
temporary of type unsigned long...

Big difference between ULong(arr) and (ULong)arr, not true?

W
 
T

Tom Widmer

werasm said:
Yes, but that was not a c-style cast :). That was instantiating a
temporary of type unsigned long...

Big difference between ULong(arr) and (ULong)arr, not true?

No, the two are semantically identical - see 5.2.3 in the C++ standard.
For example:

typedef int* p;
int i = 1;
p pi = p(i); //meant to do p(&i)

Sadly, that is well-formed C++.

Tom
 
W

werasm

Tom said:
No, the two are semantically identical - see 5.2.3 in the C++ standard.
For example:

typedef int* p;
int i = 1;
p pi = p(i); //meant to do p(&i)

Sadly, that is well-formed C++.

Thanks (I've learnt), Yes truly sad (honestly - why?). In that case I'd
rather use:

cout << "0x" << hex << reinterpret_cast<ULong>(arr); //...or better
std::cout << "0x" << std::hex << static_cast<void*>(arr) << std::endl;

Werner
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top