c++ - character printing confusion -


i have code:

#include <iostream> #include <string> using namespace std; int main(){     char abc [20] = "hello hello hi";     char* ptr = abc;     cout << (abc+3); return 0; 

}

why print out third character , not third character?

-edit- whoever flagged down. it's not same prinf(), same type of concept. didnt know nuances

to understand why, have understand bit of pointer arithmetic.

abc same &abc[0] , (abc + 3) same &abc[3]

that being said, cout prints string given char * null character.

therefore, printing string starts @ third character end of string. if wanted print third character, dereference pointer third character this.

 *(abc + 3) 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -