Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Array expression evaluation  RSS feed

 
ahmed yehia
Ranch Hand
Posts: 424
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Source: Enthuware v4


In an array access, the expression to the left of the brackets appears to be fully evaluated before any part of the expression within the brackets is evaluated.

I m not getting this concept.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy!
What is
a [ (a = b)[3] ] ?

It is the (a = b)[3] -th element of array a.

With a = b, the variable a now points to the array object of the second line (where also b points to).
so (a = b)[3]
points to the 3 rd element of the b array. The zero.

The total expression
a [ (a = b)[3] ]
is the same as
a [0]

But didn't we reassign a to b in the inner part?
Yes, but:

In an array access, the expression to the left of the brackets appears to be fully evaluated before any part of the expression within the brackets is evaluated.
.
The chosing of array a has been done before the reassignment inside the brackets. Therefore the one and not the two is printed.


By the way, after that line the reassignment will be valid for following code.

prints
1
1
2





Yours,
Bu.
 
Gautam Pandey
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I agree with the above explanation , but when i first look at the code , i took it this way
(a = b)[3] ==> a[3]=b[3] ==> a[3]=0 ==> 0
so a [ (a = b)[3] ] ==> a[0]
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gautam Pandey:


I agree with the above explanation , but when i first look at the code , i took it this way
(a = b)[3] ==> a[3]=b[3] ==> a[3]=0 ==> 0
so a [ (a = b)[3] ] ==> a[0]



(a = b)[3] ==> a[3]=b[3]
No, here is the mistake. You don't reassign individual elements of the arrays with (a=b)[3]


Yours,
Bu.
 
Gautam Pandey
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it !
The (a=b) is evaluated first then the index [3] is applied to the new reference array (now a is pointing to b) so now it's a[3] which is actually b[3]
 
Kumar Khiani
Ranch Hand
Posts: 40
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the wonderful explanation
 
Mian Amjad
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... But element 3 in b[2,3,1,0] is 0. Why the out put is 1?
 
Henry Wong
author
Sheriff
Posts: 22840
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mian Amjad wrote:... But element 3 in b[2,3,1,0] is 0. Why the out put is 1?


Element three is zero... okay. You got that... Then you are using zero to dereference the "a" array -- and the zeroth element is one.

There is also another point... the side effect of changing the "a" array reference to point to the same array as the "b" array reference doesn't happen until after the "a" reference is dereferenced, so, it is using the previous array (before the side effect).

Henry
 
Mian Amjad
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry, so the println uses index [3] of b arry (which is 0) to retrieve value from array a (a[0]), which is 1. Got it. 
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!