+ 6
How the code actually work?
cout << 4 + "sololearn"; Output: learn
11 Answers
+ 7
4+"sololearn" is like:
char str[] = "sololearn";
cout << str[4];
It starts outputing at the 'l' and goes until the '\0' after the 'n'.
+ 4
John Wells Thanks for the explanation. it really helpfull...
+ 2
John Wells btw, does cout << str[4] will only give an output 'l'
+ 2
John Wells yup, that true..
+ 2
My brain finally decided to work. I left out taking the address of to make it a string pointer instead of a character. This does the same output as your original:
char str[] = "sololearn";
cout << &str[4];
+ 2
Xhy, I see.
I have done a bunch of C++ challenges but haven't encountered this one yet.
I am sure there's a lot of nice stuff!
+ 1
Hm, interesting ...
Seems to be a shortcut for reading the string not from position 0 ('s') but position 4 ('l') (5 + the string would give 'earn').
Never seen it before ...
+ 1
True... but you got the concept. str+4 wouldn't have made as much sense.
+ 1
It's just another writing method.
+ 1
HonFu If you do some challenges, you'll see quiz like this.
0
write code that display odd and even