-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
  • 74,985
  • 8
  • 76
  • 165
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
  • 43,454
  • 1
  • 47
  • 90