+ 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
6 Answers
+ 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
+ 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)
+ 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.
+ 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
+ 1
ok so far...
and why is there a second output also?
+ 1
Thanks a lot guys. That really helped me!