If I write the code like this below?
int arr[] = {6, 7, 8, 9, 10};
int *ptr = arr;
*(ptr++)+= 123;
what’s the elements in the arr[] now?
I originally thougt the arr[] now should be {6, 130, 8, 9, 10}, but actully the result is {129, 7, 8, 9, 10}, I don’t know why?
In my opinion, ptr++ is in the bracket, so the ptr should increase first, isn’t it? after it increased one, it should point to the second element in the array.
The value of
ptr++is the value ofptrbefore any increment (the side-effect is incrementingptrat some time during the evaluation of the expression).That is the value that is dereferenced in
*(ptr++).If you dereference
ptrin a subsequent expression, it points to the next element, the one with value7.