+ 1
C++ pointers not clear
I had certain problem in a challenge: char* a = "Coding"; char* b = (++a) + 3; cout << *b; Can someone explain why this code outputs "n" and how everything in example above works?
1 Answer
+ 6
char* a = "Coding";
char* b = (++a) + 3;
cout << *b;
a points at 'C'
++a makes a point at 'o'
+3 adds 3 to a pointing to 'n'
the pointer to 'n' is stored in b
so cout outputs 'n'
Another way to look at it.
char a[] = "Coding";
int i = 0;
char *b = a[++i+3];
cout << *b;
i is changed to 1
1+3 is 4
b is assigned a[4]
so cout outputs 'n'