+ 3

C++ Challenge Strings

I completed several challenges within the last days and I failed to understand the question inn the picture totally. Can someone help me out and explain why there is this output of 2 letters??? ------- char *prt; string Str = "abcdefg"; prt = Str; prt += 5; cout << prt: ------- Output fg I'm totally confused! Thanks mates

8th Sep 2018, 7:42 AM
Henry
Henry - avatar
6 odpowiedzi
+ 6
Strings are just char pointers (or arrays). prt = Str means prt holds a pointer to the first character in Str. And that means if you increment it by 5, it goes to the 5th element of Str
8th Sep 2018, 7:50 AM
qwerty
qwerty - avatar
+ 6
Henry char* is array and prints everything hold by that array... as pointer shifted to fifth element, it point from fifth location and prints whatever is contained (have char* as 10 letter and you will observe last five characters)
8th Sep 2018, 8:39 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 4
Because if you want to print a string, cout prints the current character and the characters after it until it reaches a null termination character '\0'. By default, any string that is defined with double quotes (") has a default null termination character in the end.
8th Sep 2018, 7:59 AM
qwerty
qwerty - avatar
+ 2
the string stored is like thi:-{a,b,c,d,e,f,g,'/0'} Now....when you inc the value of prt by 5, u are actually increasing the stored address by five ....which makes prt go on 6th element(1+5=6) i.e. f Now....when printing a string...it prints upto null char...so it print f...then g....then stops on null char
14th Sep 2018, 3:19 PM
himanshu k
himanshu k - avatar
+ 1
ok so far... and why is there a second output also?
8th Sep 2018, 7:55 AM
Henry
Henry - avatar
+ 1
Thanks a lot guys. That really helped me!
8th Sep 2018, 11:50 AM
Henry
Henry - avatar