+ 6

Could someone clarify this for me?

C++ code by Waterfall St: char *s = "string\0"; int i = 1; while (*(++s)) ++i; If anyone could please explain (in simple terms) what the while condition means (and results in), I'd appreciate it!

14th Apr 2017, 10:51 AM
AtK
2 Réponses
+ 6
You have declared a char type pointer. Such a pointer points to the base address of the char array. Here, the base address points to 's' By *(++s) you are pointing to the next value in the array, that is, 't'. This continues till the loop reaches \0 where it terminates. If you had a cout <<i; at the end of the program, it would say 6, because i=1 and 6 is length of the array, so the loop runs 6 times.
14th Apr 2017, 11:18 AM
Pixie
Pixie - avatar
+ 2
Here, the while statement means until the pointer 's' does not reach at the end of the word(so that ++S is not possible).
14th Apr 2017, 11:15 AM
DEVIL kingg
DEVIL kingg - avatar