Skip to content

Instantly share code, notes, and snippets.

@oltolm
Last active February 25, 2024 19:17
Show Gist options
  • Save oltolm/1738289b5ac866ec6a7e4ef20095178e to your computer and use it in GitHub Desktop.
Save oltolm/1738289b5ac866ec6a7e4ef20095178e to your computer and use it in GitHub Desktop.
Nim pointer arithmetic
# from https://forum.nim-lang.org/t/1188#7366 by Jehan
# most of the time only the dereference operator is needed, import it like this:
# from ptrmath import `[]`
template `+`*[T](p: ptr T, off: int): ptr T =
cast[ptr type(p[])](cast[ByteAddress](p) +% off * sizeof(p[]))
template `+=`*[T](p: ptr T, off: int) =
p = p + off
template `-`*[T](p: ptr T, off: int): ptr T =
cast[ptr type(p[])](cast[ByteAddress](p) -% off * sizeof(p[]))
template `-=`*[T](p: ptr T, off: int) =
p = p - off
template `[]`*[T](p: ptr T, off: int): T =
(p + off)[]
template `[]=`*[T](p: ptr T, off: int, val: T) =
(p + off)[] = val
when isMainModule:
var a: array[0..3, int]
for i in a.low..a.high:
a[i] += i
var p = addr(a[0])
p += 1
p[0] -= 2
echo p[0], " ", p[1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment