c++ - Addition on array of char -
this stupid question. playing c++ , find line of code makes me little confused
char buffer[7] = {'0', '1', '0', '9', '0', '1'}; // prints 010901 printf("%s", buffer); // prints 0901 printf("%s", buffer+2);
why can (+ 2) buffer variable , it's shifted 2 character right?
in context such one, array name buffer
"degrades" being pointer first value. since c , c++ support pointer arithmetic, , strings represented pointers arrays of characters, it's fine.
note terminating '\0'
character, implied since specify larger size provide initialization data for.
still, code can simplified to:
const char buffer[] = "010901";
you can visualize so:
+-----+-----+-----+-----+-----+-----+-----+ buffer ------> | '0' | '1' | '0' | '9' | '0' | '1' | '\0 | +-----+-----+-----+-----+-----+-----+-----+ ^ | | buffer + 2
Comments
Post a Comment