How the code actually work? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 6

How the code actually work?

cout << 4 + "sololearn"; Output: learn

30th Oct 2018, 10:56 PM
オレンジ
オレンジ - avatar
11 ответов
+ 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'.
30th Oct 2018, 11:56 PM
John Wells
John Wells - avatar
+ 4
John Wells Thanks for the explanation. it really helpfull...
31st Oct 2018, 12:00 AM
オレンジ
オレンジ - avatar
+ 2
John Wells btw, does cout << str[4] will only give an output 'l'
31st Oct 2018, 12:08 AM
オレンジ
オレンジ - avatar
+ 2
John Wells yup, that true..
31st Oct 2018, 12:16 AM
オレンジ
オレンジ - avatar
+ 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];
31st Oct 2018, 4:25 AM
John Wells
John Wells - avatar
+ 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!
3rd Nov 2018, 4:50 PM
HonFu
HonFu - avatar
+ 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 ...
31st Oct 2018, 12:00 AM
HonFu
HonFu - avatar
+ 1
True... but you got the concept. str+4 wouldn't have made as much sense.
31st Oct 2018, 12:13 AM
John Wells
John Wells - avatar
+ 1
It's just another writing method.
3rd Nov 2018, 4:25 PM
Xhy
Xhy - avatar
+ 1
HonFu If you do some challenges, you'll see quiz like this.
3rd Nov 2018, 4:26 PM
Xhy
Xhy - avatar
0
write code that display odd and even
4th Nov 2018, 4:20 AM
Mahamed Iise Daahir
Mahamed Iise Daahir - avatar