I know exactly what you mean, and what you expect the code to do.
But as I said, the code does exactly what I expect it to / what it is supposed to do.
When you create the variable
child, you create the name
child as a synonym for a memory address.
For simplicity's sake let's say the memory address is
0x0000, and this is just an address like your home address.
Then
child is just an entry in a citizen register:
Code: Select all
variable child pointing to address 0x0000
When you assign an integer value to that variable, its
value gets stored at that address, like becoming the resident at that address.
Now, the
value 1 is stored at the
memory address the variable
child is pointing to:
Code: Select all
memory content at address 0x0000 == 1
When you access
child, you get the content that is stored at the address it is pointing to, in this case
1.
Now, when you assign an object like an array to that variable, the memory address of that array will be stored in the variable.
Lets say, the array is stored at memory address
0x1000, then
child contains that address.
When you now access the arrays contents like
You actually access the memory at
0x1000 plus a value for the index, the compiler/interpreter does this for you, it knows through that syntax
that you try to access memory indirectly through the address that is stored in
child.
When you set a
value to an array element as in
the value
123 gets stored at
0x1000 (plus the value for the index).
Since you never changed the address that child is pointing at (the array at
0x1000), you still access the same memory.
When you uncomment your "fix", you assign a new array's address (let's say
0x2000) to
child.
Now, when you set a value to
you actually change the contents of the memory at
0x2000 (again + index).
I feel like this is a much more complicated description than the first, but maybe it helps.