The current state of the documentation might be confusing as the address of the first copied variable (i) matches the address of the loop variable (i) in the previous example. This may cause confusion into thinking that the first copy of (i) takes the same address as the loop variable, which is not the case.

Ihab Abdelkareem 2023-03-14 22:31:21 +00:00
parent 92573d60e5
commit 747b4f22e8

@ -44,7 +44,7 @@ The new output of the program is what was expected:
```
Values: 0 1 2
Addresses: 0x40e020 0x40e024 0x40e028
Addresses: 0x40e024 0x40e028 0x40e032
```
Explanation: the line `i := i` copies the loop variable `i` into a new variable scoped to the for loop body block, also called `i`. The address of the new variable is the one that is appended to the array, which makes it outlive the for loop body block. In each loop iteration a new variable is created.