-=> Quoting Neil Heller to Darin Mcbride
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
I'd have a hard time believing this was a dishonest question. ;-)
NH> to char, would you need to cast it in order to print the address
NH> (contents of the pointer).
You'd cast it to (void*) because, well, there is an operator for inserting
pointers that takes a void* as well as one for char*:
class ostream {
//...
ostream& operator<<(void*);
ostream& operator<<(char*);
//...
};
The first one prints out the address (since if it is a void*, you don't know
how to represent what it's pointing at). The second, knowing what the
pointer is pointing at, prints out the array (C string) that it points at.
NH> Instead of a C-style cast, would it be preferred in that situation to
NH> do:
NH> cout aPtr << end;
Not quite the same result. ostream::operator<<(void*) usually prints out in
hex. On 16-bit systems in large or huge memory model it will usually print
out the segment, too, in standard style (i.e. SSSS:OOOO). int will usually
be in decimal (unless, of course, the ostream was previously informed
otherwise). So you'd want the C++ style of:
cout (aPtr) << endl;
(Notice the ()'s - from what I can tell, they're required.)
Hope this helps,
... No. You cannot call 911. I am downloading my mail!
--- FastEcho 1.46
---------------
* Origin: House of Fire BBS - Toronto - (416)601-0085 - v.34 (1:250/536)
|