c++ - Index of pointer to a char array -
c++ - Index of pointer to a char array -
i trying find out, why output of next programme "wahnahnn".
my question is: why index rising above p[4] (i.e. exceed length of array) , why produce "ahnn" after "wahn"?
i still confused difference between p+i (which should location) , *(p+i) should value. why output value in both cases?
#include <iostream> using namespace std; int main() { char a[] = "wahn"; char *p = a; (int i=0; p[i]; i=i+1) switch (i%2) { case 0: cout << p+i; break; case 1: cout << *(p+i); break; } homecoming 0; }
the type of p + i
char*
.
the operator <<
interprets nullterminated string. means programme output "whan" (i == 0
p + == p
, wich same a
) first iteration, "a" sec iteration, "hn" 3rd , "n" lastly one.
c++ arrays pointers
Comments
Post a Comment