+ 1

String addition?

So a question in a C++ challenge asked what the output of cout << 1 + "hello"; (or whatever the string was, it's not important) would be, I went to find out and the output was "ello" Why?

19th Feb 2019, 7:56 AM
Daniel Cooper
Daniel Cooper - avatar
2 Answers
+ 2
"hello"'s type is const char *. Pointer's pointing to first letter. +1: second letter.
19th Feb 2019, 8:04 AM
HonFu
HonFu - avatar
+ 1
This is something called pointer arithmetics... When you define a literal string, compiler get you a pointer to a sequence of characters in memory. This pointer is an adress, a number, and as such you can add numbers to it. Adding a number here mean "get the adress of char advancing by i chars"
19th Feb 2019, 8:54 AM
KrOW
KrOW - avatar