Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Before I confuse myself... (pointer things...)

Status
Not open for further replies.
Feb 14, 2002
88
JP
I've still not gotten my Accelerated C++ book, but I've had some free time at work, so I figured web tutorials wouldn't hurt. I've gotten up to a tutorial on pointers. Since my only programming experience is shell scripting, pointers is a bit foreign to me.... never done (or needed to) do it before.

Basically, here's an excerpt from the page I'm using.

char * terry = "hello";
"Assuming that in memory, terry starts at block 1702: it is important to indicate that terry contains value 1702 and not 'h' nor "hello", although 1702 points to these last ones."

So, in that case, I tried the following:
cout << terry << endl; // hello
cout << *terry << endl; // h
cout << &terry << endl; // 0x80498c4

Should I assume if you want to find out &quot;where&quot; something is stored in memory, must always preclude it w/ the ampersand, and that even though a pointer carries this location, it will output the value?

Thanks
 
char* is treated specially by ostreams like cout. Whereas a normal pointer would output its value (an address), a char* outputs outputs the string it points to.

So your &quot;&terry&quot; is actually giving you the address in memory of the pointer.

If you want the char* to be output as an address instead of a string, you have to cast it to void* first.

Try your experiment with any pointer other than a void*, char*, or wchar_t* and you'll get the expected results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top