Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

char* - why is there no address in the pointer?

Tags:

c++

I have a basic question about char* I don't understand

char* aString = "Hello Stackoverflow";

The Pointer points at the first character of the character chain.

cout << *aString; // H

but why is the whole string saved in the pointer?

cout << aString //Hello Stackoverflow

I would expect an address, aren't addresses saved in pointers? Where is the address of "Hello Stackoverflow"?

Any help much appreciated

like image 336
最白目 Avatar asked Nov 25 '12 13:11

最白目


1 Answers

There is an overload for operator<<(ostream&, char const*) which output the null-terminated string starting at that pointer and which is preferred to the operator ostream::operator<<(void*) which would have output the address.

If you want the address, cast the pointer to void*.

like image 157
AProgrammer Avatar answered Oct 12 '22 19:10

AProgrammer