+ 5

whats the answer and how does it work?

Char *p="sololearn" ; cout<<p+1;

26th Jan 2017, 12:00 PM
Shravan Kumar
Shravan Kumar - avatar
16 Réponses
+ 9
̶Y̶o̶u̶r̶ ̶c̶h̶a̶r̶a̶c̶t̶e̶r̶ ̶t̶y̶p̶e̶ ̶p̶o̶i̶n̶t̶e̶r̶ ̶i̶n̶i̶t̶i̶a̶l̶l̶y̶ ̶p̶o̶i̶n̶t̶s̶ ̶t̶o̶ ̶t̶h̶e̶ ̶f̶i̶r̶s̶t̶ ̶c̶h̶a̶r̶a̶c̶t̶e̶r̶ ̶i̶n̶ ̶t̶h̶e̶ ̶s̶t̶r̶i̶n̶g̶.̶ ̶T̶h̶e̶n̶ ̶y̶o̶u̶ ̶i̶n̶c̶r̶e̶m̶e̶n̶t̶ ̶i̶t̶ ̶b̶y̶ ̶o̶n̶e̶,̶ ̶s̶o̶ ̶t̶h̶e̶ ̶o̶u̶t̶p̶u̶t̶ ̶w̶i̶l̶l̶ ̶b̶e̶ ̶'̶o̶'̶.̶
26th Jan 2017, 12:06 PM
Hatsy Rei
Hatsy Rei - avatar
+ 7
Shouldn't it be cout<<*(p+1);?
26th Jan 2017, 12:15 PM
Filip
Filip - avatar
+ 7
If you type cout<<p it will print the whole word, and if you add 1, like p+1 the word will start from the second letter (o) in this case
26th Jan 2017, 12:19 PM
Filip
Filip - avatar
+ 7
Write it like this *(p+n--) And the explanation for your case is that it takes the ASCII value of 's' because thats *p and adds 2 on it, so the output is 117
26th Jan 2017, 12:29 PM
Filip
Filip - avatar
+ 7
No problem
26th Jan 2017, 12:34 PM
Filip
Filip - avatar
+ 6
@Filip halp me on this one =^=
26th Jan 2017, 12:15 PM
Hatsy Rei
Hatsy Rei - avatar
+ 6
Ohhh, with the explanation?
26th Jan 2017, 12:17 PM
Filip
Filip - avatar
+ 6
Yes, *p prints just one character, and p prints the whole word, because you wrote it like char *p="sololearn";
26th Jan 2017, 12:23 PM
Filip
Filip - avatar
+ 6
I've explained it all in code, so if you want check it out. It's related to your questions: https://code.sololearn.com/cz9JBW1SOgvB/?ref=app
26th Jan 2017, 12:50 PM
Filip
Filip - avatar
+ 3
but when it's cout<<*(p+1) it's just o ??
26th Jan 2017, 12:22 PM
Shravan Kumar
Shravan Kumar - avatar
+ 3
thank you
26th Jan 2017, 12:32 PM
Shravan Kumar
Shravan Kumar - avatar
+ 2
but the output is ololearn according to compiler
26th Jan 2017, 12:11 PM
Shravan Kumar
Shravan Kumar - avatar
+ 2
no just cout <<p+1
26th Jan 2017, 12:16 PM
Shravan Kumar
Shravan Kumar - avatar
+ 2
yes please I don't how it works
26th Jan 2017, 12:17 PM
Shravan Kumar
Shravan Kumar - avatar
+ 2
last question I initialize a variable INT n=2; cout<<*p+n--; output is 117 ??
26th Jan 2017, 12:27 PM
Shravan Kumar
Shravan Kumar - avatar
+ 1
wow I understood completely
26th Jan 2017, 12:59 PM
Shravan Kumar
Shravan Kumar - avatar