what is the result of a[++i] = i ; ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mxy0226
    New Member
    • Apr 2013
    • 1

    what is the result of a[++i] = i ; ?

    if a[3]={-1,-2,-3};
    i=0;
    what a[++i] = i work?
  • vijay6
    New Member
    • Mar 2010
    • 158

    #2
    Hey mxy0226,

    The value of 'i' (1 - One) will assign to a[1].

    Workflow:

    1) This statement evaluate from right to left.

    2) In the right hand side, no changes. The value of 'i' remain as it is (i=0).

    3) In the left hand side, first '++i' statement will execute. i.e., The value of 'i' incremented by 1. Now 'i' is 1.

    4) Finally the assignment statement execute. a[1] = 1 (Because now 'i' value is 1).

    5) After that statement (a[++i] = i) the array 'a' will look like this a[3] = {-1, 1, -3};

    Comment

    • VanessaMeacham
      New Member
      • Sep 2012
      • 31

      #3
      HI ! this operator perform right to left so vijay6's answer is right. it also overloads.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Actually the behaviour is undefined because you are accessing a variable (i) more than once between sequence points where one of those accesses modifies the value of the variable.

        You do not know what order the 2 sub-expressions of the = operator are evaluated in, it is not specified in the standard.
        Last edited by Banfa; Apr 18 '13, 01:46 PM.

        Comment

        Working...