tags:

views:

124

answers:

5

assume

char a='s';

now i want to store the address of variable a in char pointer p

char *p=&a;

if i use cout << p; then i get the value s, not the address!

can anyone explain the reason why is it so?

+9  A: 
KennyTM
didn't Kenny give an explanation in his first sentence?
codymanix
@codymanix Now there is a link, when I voted down there wasn't. The link is perfect but what he wrote is rubbish. `char *` is not specialized to string and do nothing. He writes that it outputs something what is absolutely wrong. To the char * I can save every byte array I want. And the correct answer to the question is the operator. That is the reason. Now I am just laughing how the senseless answer can acquire high rating
Gaim
@gaim No it is not rubbish, it's just written in a confusing way, but the << operator for char* is specialized to treat it like a null-terminated string. Really, downvoting for this is a little bit ridiculous, just note it in a comment that his sentence is constructed a little bit confusing.
KillianDS
There's no way that he printed 's' as a string, because it's not null-terminated.
DeadMG
@DeadMG, it is when the memory after it is zeroed out ...
KillianDS
@KilianDS At first he didn't append the link. He just wrote that the char * is specialized to output a string what isn't. And about specialized operator he ( still ) wrote nothing
Gaim
@DeadMG Read my answer which was voted down without reason and is correct
Gaim
KillianDS
@KilianDS I agree with you now it is quite silly. The problem is that the wrote nothing about the `<<` and I think that it confused the questioner because it is not transparent for the beginner. And about it he wrote nothing. Apparently I am the one who see it but that's the life.
Gaim
A: 

In C/C++ a pointer to char normally is used as a pointer to a null-terminated string. The << operator handles this common use case.

ur
A: 

Calling cout << p is type safe ( C++ objects and operators ) so it uses a operator for char *. This operator serves to print the strings saved in the char pointer. It takes the address and prints everything from start to the first occurrence of byte zero. If you are using MS windows then it is probably only this one char because this system zeros the memory so zero byte is soon. If you want to print the address then try cout << (void *)p;

Gaim
thanks guys, i got the point !
Pink F
Don't cast to int to print out the actual pointer value of a `char*` as this can result in truncated output in 64-bit apps. Cast it to `void*` instead.
Mark B
@Mark OK, thanks
Gaim
A: 

In C a char* is typically used to point to the beginning of a (null-terminated) string. In order to make C++ IO streams (such as cout) compatible with this habit, there's an overloaded verion of operator<<() for handling char pointers. This operator just prints out the null-terminated string starting at the given address.

If you want to print out the 'value' of the pointer (note that it is not part of the C/C++ standard what a pointer 'looks like'), cast the pointer to void* such as

std::cout << static_cast<void*>(charpointer);
MartinStettner
Thanks that you summarized what we wrote.
Gaim
A: 

As many answers above tell you, << operator is overloaded to handle char* as C type string i.e NULL terminated array of characters. In your case when you print p, it gets treated as C string and all the characters till first NULL character are printed. If you only get s printed then there is a NULL character immediately next to your variable a in memory. But this behavior may change depending on the memory contents. I guess you are declaring a as a global variable because space for global variables is usually initialized to all zeros.

Also it will be an interesting experiment for you to declare two character variables and then cout there pointer the same way as you have above. My guess is that pointer to first variable will print both of them.

binW