The addr operator

The addr operator returns the address of an l-value. If the type of the location is T, the addr operator result is of the type ptr T. An address is always an untraced reference. Taking the address of an object that resides on the stack is unsafe, as the pointer may live longer than the object on the stack and can thus reference a non-existing object. One can get the address of variables. For easier interoperability with other compiled languages such as C, retrieving the address of a let variable, a parameter, or a for loop variable can be accomplished too:

  1. let t1 = "Hello"
  2. var
  3. t2 = t1
  4. t3 : pointer = addr(t2)
  5. echo repr(addr(t2))
  6. # --> ref 0x7fff6b71b670 --> 0x10bb81050"Hello"
  7. echo cast[ptr string](t3)[]
  8. # --> Hello
  9. # The following line also works
  10. echo repr(addr(t1))