I am trying to find out, why the output of the following program is "WAHNAHNN".
My question is: Why does the index i rise above p[4] (i.e. exceed the length of the array) and why does it produce another "AHNN" after the "WAHN"?
I am still confused with the difference between p+i (which should be a location) and *(p+i) which should be a value. Why is the output a value in both cases?
#include <iostream>
using namespace std;
int main()
{
char a[] = "WAHN";
char *p = a;
for (int i=0; p[i]; i=i+1)
switch (i%2) {
case 0: cout << p+i;
break;
case 1: cout << *(p+i);
break;
}
return 0;
}
operator <<onostreamis overloaded forchar *as you can see in the documentation