Q:

Why does this int and string concatenation give this output in C++

for(int i=0;i<10;i++)
{
  cout << i+"passs" << "\n";
}

This produces output as follows:
passs
asss
sss
ss
s

Now my thinking is that we are seeing a pointer manipulation where the counter i gets added to passs char pointer and we are seeing output from "passs" pointer+0,"passs" pointer+1 etc.
Question beckons as to why it does not stop when it reaches null character, coz it prints 5 blank lines.
Next is the cout << "passs"+i << "\n"; prints the same thing. I thought this should have printed all blank lines from second position as this time the passs pointer should have been probably from last position. On reading what I wrote here, it probably makes sense to see that the output for the 2nd cout would be the way it is.
I wanted to know if I'm thinking correctly and if there is more to this which I've not comprehended?

A:

You're right.
The string literal is interpreted as a pointer to the start of the string. Adding an int to this is a pointer increment.
Once you go past the end, you're into undefined behaviour - it seems fairly harmless on your system, but might end in a coredump on another.

A:

Your initial reasoning is correct.  You're doing pointer arithmetic here, so the first six lines (the sixth being \0\n) of output are good.  But you get into trouble when you walk off the end of the string.  There's nothing in your for-loop to stop the iteration, so your program theoretically could do anything with "passs" + 6, 7, 8, and 9.  Let's look into why.
Recall that a string literal has type const char *.  At some spot in memory determined by the compiler, you have:
| p | a | s | s | s | \0 | * | * | * | * |

The * symbols represent a no-man's land of indeterminate memory past the end of your string.  As your for-loop runs, it starts at p and walks forward.  Since we're addressing a const char *, each step will be one byte.  At each step, cout tries to print as much as it can until reaches a null character (as you see in the output).  But you're not allowed to try to print anything from no-man's land.  Doing so results in undefined behavior.  In your case, that memory contained unprintable characters.
So in summary, you were mostly correct.  cout did stop printing when it reached a null.  But the for-loop didn't.

