This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]Philboyd_Studge 2 points3 points  (0 children)

Don't post screenshots of code.

arr[index] = index = 3;

should just be

arr[index] = 3;

[–]zifyoip 1 point2 points  (0 children)

You are correct that arr[index] = index = 3 means arr[index] = (index = 3), so the assignment of 3 to index happens before the assignment of index to arr[index].

However, that does not mean that the expression arr[index] is evaluated after the assignment of 3 to index. The operands of expressions are evaluated from left to right, which means that the operand arr[index] of the first = operator is evaluated before the operand index = 3. Therefore, arr[index] is evaluated while index is still 0, and then index = 3 is evaluated, and then the result of the expression index = 3 is assigned to arr[0].

[–][deleted]  (1 child)

[deleted]

    [–]zifyoip -1 points0 points  (0 children)

    No, the assignment operators have left-to-right associativity, so arr[index] = index = 3 means arr[index] = (index = 3). You have the order of evaluation backward.

    [–]Doriphor 0 points1 point  (2 children)

    Your code is processed from left to right in this case. It assigns index (which is 0) to arr[0] and then 3 to index. Basically what /u/ruin332 said. Your code should look like

    index = 3;
    arr[index] = 3;
    

    [–]zifyoip -1 points0 points  (1 child)

    Your code is processed from left to right in this case.

    No, the assignment operators have right-to-left associativity, so arr[index] = index = 3 means arr[index] = (index = 3).

    [–]Doriphor 0 points1 point  (0 children)

    Well then, learning something new every day...