NH> char aStr[10] = "abcdefgh";
NH> char * aPtr = &aStr[5];
NH> cout << aPtr << endl;
DM> You want:
DM> cout << (void*)aPtr << endl;
NH> Just an honest question: why, after aPtr was declared to be a pointer
NH> to char, would you need to cast it in order to print the address
NH> (contents of the pointer).
There are many overloads for operator <<. There is one for int's,
one for char's, one for char*'s, etc.
The overload for the char* is designed to handle zero terminated
character arrays. But there is also an overload defined to handle
a void*. That version will print the value of the pointer.
NH> Instead of a C-style cast, would it be preferred in that situation to
NH> do:
NH> cout aPtr << end;
I don't believe that will compile. reinterpret_cast would compile.
But you may well get a different output (thought the same value.) On
my machine, the operator<<(void*) displays the answer in "pointer
format" while operator<<(int) displays "plain old numbers".
---
þ Blue Wave/QWK v2.12 þ
---------------
* Origin: St. Louis Users Group (1:100/4)
|