+ 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?

15th Mar 2018, 9:33 PM
Vladi Petrov
Vladi Petrov - avatar
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'
15th Mar 2018, 9:48 PM
John Wells
John Wells - avatar