Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer operations and operator precedence in C

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.

like image 339
Unheilig Avatar asked Jan 06 '14 18:01

Unheilig


People also ask

Which has higher precedence in C * or and?

Order of Precedence in Arithmetic Operators ++ and -- (increment and decrement) operators hold the highest precedence. Then comes * , / and % holding equal precedence. And at last, we have the + and - operators used for addition and subtraction, with the lowest precedence.

What is the priority among (* %) (+ -) and (=) C operators?

1) What is the Priority among (*, /, %), (+, -) and (=) C Operators.? Explanation: Assignment operator in C has the least priority.

What is operator and operator precedence?

Operator precedence determines how operators are parsed concerning each other. Operators with higher precedence become the operands of operators with lower precedence.


1 Answers

The statement

*intAPtr++ = *intBPtr++;

is parsed as

*(intAPtr++) = *(intBPtr++);

and breaks down as follows:

  • The value currently pointed to by intBPtr (3) is assigned to the location pointed to by intAPtr (intgA[0]);
  • The pointers intAPtr and intBPtr are incremented.

The exact order in which these things happen is unspecified; you cannot rely on intBPtr being incremented after intAPtr or vice-versa, nor can you rely on the assignment occuring before the increments, etc.

So by the time this is all done, intgA[0] == 3 and intAPtr == &intgA[1] and intBPtr == &intgB[1].

The expression a++ evaluates to the value of a before the increment.

like image 104
John Bode Avatar answered Oct 13 '22 21:10

John Bode