0

To recreate this little problem of mine use this code (Yes it's bruteforced and could be slimmed down):

char hello[] = "John";
char *ptr1 = &hello[0];
char *ptr2 = &hello[1];
char *ptr3 = &hello[2];
char *ptr4 = &hello[3];
std::cout << ptr1 << "$";
std::cout << ptr2 << "$";
std::cout << ptr3 << "$";
std::cout << ptr4 << "$";

as you may see after trying this code, it returns the following:

John$ohn$hn$n$

I have no idea why it takes all elements after the specified one.

Any help?

5 Answers 5

1

It's because you're printing the pointer, not the thing it points to. And char pointers in C programs often point to the start of a char[] which represents a '\0'-terminated string. That is why std::ostream (of which cout is an instance) has an overloaded << operator that outputs the entire string, up to the terminator.

To print just the character, dereference the pointers first:

std::cout << *ptr1 << "$";

You could also use references instead, which are treated largely as a substitute for the value itself:

char &ptr1 = hello[0];
Sign up to request clarification or add additional context in comments.

Comments

1

ptr1 through ptr4 are also pointers to characters, therefore << will treat them as strings, and print them as such, giving you what you saw.

If you just want the characters at each location, lines of the form:

char ch1 = hello[0];

will work, as will changing the output to:

std::cout << *ptr1 << "$";

1 Comment

Just saw your answer after I saw Thomas's. Thanks for the quick response.
1

if you want to print value at particular address then :

std::cout << *ptr1 << "$";
std::cout << *ptr2 << "$";
std::cout << *ptr3 << "$";
std::cout << *ptr4 << "$";

Comments

1

Your question seems to be based on some completely unfounded expectations. It looks like you expected your code to print a single character for each cout << ... line. But why did you expect it to do that? What you send to cout in your code is actually a char * pointer. Why did you expect cout to dereference that pointer for you and print the character? A normal thing to expect would be to see the pointer value in the output (the address), not the character value.

So, the question you should really be asking is not "why it prints several characters from my array?". The question you should really be asking is "why it prints any of my characters at all?". You never dereferenced your pointer. Yet, instead of pointer value the output contains a sequence of char objects that pointer was pointing to. I.e. apparently cout decided to dereference your pointer for you, even though you never explicitly asked it to. That is the question you should be asking: why did my pointer get dereferenced at all?

And the answer is that [const] char * pointers receive special treatment when sent to cout using << operator. There's a dedicated overloaded version of << operator introduced specifically for that purpose. That version of << operator will treat char * pointers as pointing to the beginning of a C-string. And that operator will output the entire C-string all the way to the zero terminator character. That is exactly what is happening in your experiment.

Note that if you use any other pointer type (like int *) for example, you will not see the pointed value (or values) in the output. You will see the implementation-dependent representation of the pointer itself.

Comments

0

I am not entirely sure but if it cout receives a char pointer, it will print, everything until it reaches a blank in memory, (C style character string), and if you just want to print a character, you should print only the content of the variable, that means just pass the content not the memory location...

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.