Need clarification

Why is it that these 2 provides the same result for x?

given that m = 10, n = 15

1
2
 x = m - 2*n--;
 x = m - 2*n;


So that I dont make a new topic, this line of code here:
1
2
3
4
5
6
7
char array[11] = "0123456789";
char *ptr1 = array;
char *ptr2 = &array[1];
cout << *(ptr1+2) << endl;
cout << *(ptr1 += 3) << endl;
cout << *(ptr1+2) << endl;
cout << ptr2+1;


How come although two of the cout are the same, *(ptr +2), however the first shows 2 but the second one shows 5?
Last edited on
The only thing in which they differ is that n is decremented after the first statement, but result (x) is not affected by that
Last edited on
How come although two of the cout are the same, *(ptr +2), however the first shows 2 but the second one shows 5?


The first time *(ptr1+2) is executed it is 2 yes.
On next line you increment *ptr1 by 3.
then you print *(ptr1+2) again.
And last time i checked 3 + 2 is equal to 5. :-)
Last edited on
Topic archived. No new replies allowed.