Background
Just had a chat with a C guy today and we disagreed on the following:
int intgA[2] = { 1, 2 };
int intgB[2] = { 3, 5 };
int *intAPtr = intgA;
int *intBPtr = intgB;
So when we do:
*intAPtr++ = *intBPtr++;
My analysis
First:
intBPtr
increments by one, now pointing to the address of 5.
Then, deference, holding the value 5;
intAPtr
also increments by one, now pointing to the address of 2.
Subsequently referencing and the value is 2;
Lastly:
2 is replaced by 5.
So respectively they are: 5 and 5.
His analysis
The value of *intBPtr
is first assigned to *intAPtr
first.
Hence, they become: 3 and 3.
Then both *intAPtr
and *intBPtr
is incremented by one.
So, respectively they become: 4 and 4.
My Assumption
I thought the ++
operator takes precedence both over *
and =
, hence my assertion.
For example if we had:
*intAPtr++;
The result should be 2, right? Because we first increment the pointer and then dereference.
So why in the above case, as he claims, we first assign the value of intBPtr
to the value of intAPtr
and increment the values last?
After having taken all suggestions here, I ran the code in IDE and the result confirms that of @sujin:
Although it confirms that I was right at least in terms of precedence:
That: *intAPtr++ = *intBPtr++;
intAPtr++
has a higher precedence, which leads to: intAPtr
increments its address by 1.
Now pointing to: the address of 2.
And likewise:
intBPtr++
also increments by 1 (address).
Now pointing to: the address of 5.
Then it's *
's turn:
So the both get dereferenced (*) to respectively 2 and 5.
But the problem exists still because the assignment above (=
) did not seem to take place.
If it did both would become 5.
Looking forward to being further enlightened.