Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# what will be the output of this two dimentional array and why

ravi suthar
Greenhorn
Posts: 17

Christophe Verré
Sheriff
Posts: 14691
16
Did you try to execute the code ?

ravi suthar
Greenhorn
Posts: 17
Christophe Verré wrote:Did you try to execute the code ?

i am expecting the output will print 3
but the actual output is 5
i am confused why it is so

Christophe Verré
Sheriff
Posts: 14691
16
Can guess what a[2]=a[0]; is doing ?

ravi suthar
Greenhorn
Posts: 17
Christophe Verré wrote:Can guess what a[2]=a[0]; is doing ?

a[2] is start referring to a[0]
both a[2] and a[0] have the same reference
and there indexing would be 0 1 2 3 4
so the sum would be 0+1+2=3 ???

Christophe Verré
Sheriff
Posts: 14691
16
When you start writing to a[2], what happens to the elements in a[0] ?

Campbell Ritchie
Sheriff
Posts: 50258
79
You are correct about the notion of assignment, but don’t say reference. You have an int[], which is an array of primitives. The contents of a variable pointing to a primitive is not a reference. It is the actual value. If you write int i = 2; the actual value in the memory location pointed to by i is 2.

ravi suthar
Greenhorn
Posts: 17
when i=0 value of a[0][3] will be 3
and when i=2 value of a[2][3] will be 5
both a[2] and a[0] reffered at same.
so value 3 is replaced by 5
it means every value of a[0] is replaced by a[2]

Henry Wong
author
Marshal
Posts: 21508
84
Campbell Ritchie wrote:You are correct about the notion of assignment, but don’t say reference. You have an int[], which is an array of primitives. The contents of a variable pointing to a primitive is not a reference. It is the actual value. If you write int i = 2; the actual value in the memory location pointed to by i is 2.

I think the OP used the terms "refering" and "reference" in regards to an int[] (elements of int[][]) -- and in that context, "reference" is correct.

Henry

Campbell Ritchie
Sheriff
Posts: 50258
79
Yes, you are right Henry. I didn’t notice that. Well done noticing, and I am sorry about my mistake.