-2

I am doing some exercises in C++ when I came upon something not so clear for me:

cout << "String" + 1 << endl;

outputs : tring

I suggest it is something with pointer arithmetic, but does that mean that everytime I print something in quotes that is not part of previous defined array,I actually create a char array ?

stoychos
  • 29
  • 1
  • 5

2 Answers2

7

A quoted string (formally a string literal) is an array of const char, regardless of whether your printing it or doing anything else with it.

Pete Becker
  • 72,338
  • 6
  • 72
  • 157
5

Code:

cout << "String" + 1 << endl;  

has the same effect as this:

const char *ptr = "String";
cout << ptr + 1 << endl;

so no you do not create a new array, you just change pointer and pass it to std::cout

Slava
  • 42,063
  • 1
  • 43
  • 86