2

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?

5 Answers 5

8

The basic_ostream's << operator is specialized for const char* arguments*, to output it like a C string**. See the answer to Why does cout print char arrays differently from other arrays? for more detail.

Basically, you could cast p to void* when printing to get the address.

cout << static_cast<void*>(p);

*: Also const charT* where charT is the character type adopted by the basic_ostream, and const unsigned char* and const signed char* if the charT == char. For example, wcout would treat a const wchar_t* as a (wide) C string, but cout would treat it as a pointer.

**: This is overiddable by the character traits used by that basic_ostream. See C++ standard §[ostream.inserters.character]/4 for the detailed algorithm.

Sign up to request clarification or add additional context in comments.

8 Comments

didn't Kenny give an explanation in his first sentence?
@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 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.
There's no way that he printed 's' as a string, because it's not null-terminated.
@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
|
1

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;

1 Comment

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.
0

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);

Comments

0

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.

Comments

-1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.