tonetheman's blog

nim new and printing the address of a new variable

I never can remember how to print the address in nim. The example below shows that.

import strutils

var b = int.new()

# deref it should be zero
echo("value of b is: ", b[])

# get addr and cast it to an int, then call toHex on it from strutils
echo("hex is: ", cast[int](addr(b)).toHex())

# same as above
echo("hex is: ", toHex(cast[int](addr(b))))

# call repr on whatever addr returns
echo("addr of b is: ", addr(b).repr())

let i = cast[int](addr(b))
echo("i ", i)
echo("i in hex: ", toHex(i))