Pointers in Nim
Nim supports pointers, allowing you to pass references to values and records within your program.
In this example, we demonstrate how pointers work in contrast to values using two procedures: zeroval
and zeroptr
.
The zeroval
procedure takes an int
parameter, so arguments are passed to it by value. This means zeroval
receives a copy of the value, distinct from the one in the calling procedure.
The zeroptr
procedure, on the other hand, takes a ptr int
parameter, which is a pointer to an int
. Inside the procedure, we use the iptr[]
syntax to dereference the pointer and access or modify the value it points to.
In the main
procedure, we create a variable i
and demonstrate the difference between passing by value and passing by reference:
- We pass
i
tozeroval
, which doesn’t change the originali
. - We pass
addr i
(the address ofi
) tozeroptr
, which does change the originali
because it has a reference to the memory address of that variable.
Finally, we print the pointer itself, which is the memory address of i
. In Nim, we need to cast the pointer to an integer to print its raw value.
To run this program, save it as pointers.nim
and use the Nim compiler:
Note that zeroval
doesn’t change the i
in main
, but zeroptr
does because it has a reference to the memory address for that variable.