+ 1

Why is cout << 5+"Sololearn" output earn?

need help

11th Aug 2017, 9:52 PM
HJ🐓TDM
2 Answers
+ 6
Because "Sololearn" is actually just a pointer to a null terminated string. Since you are adding 5 to the string, you are offsetting the pointer by 5 bytes. Hence you get "earn", which is 5 bytes in from the beginning 'S'
11th Aug 2017, 10:02 PM
aklex
aklex - avatar
+ 1
the string is an array, a pointer to the first value of a null-terminated string 'S','o','l','o','l','e','a','r','n','\0' 0 , 1 ,2, 3 ,4, 5 , 6 , 7 , 8 , 9 the pointer points to 'S', so 0+5=5, now the pointer points to 'e' and the array starts from there
12th Aug 2017, 2:26 AM
Andrés04_ve
Andrés04_ve - avatar