pointerof

The pointerof expression returns a Pointer that points to the contents of a variable or instance variable.

An example with a variable:

  1. a = 1
  2. ptr = pointerof(a)
  3. ptr.value = 2
  4. a #=> 2

An example with an instance variable:

  1. class Point
  2. def initialize(@x : Int32, @y : Int32)
  3. end
  4. def x
  5. @x
  6. end
  7. def x_ptr
  8. pointerof(@x)
  9. end
  10. end
  11. point = Point.new 1, 2
  12. ptr = point.x_ptr
  13. ptr.value = 10
  14. point.x #=> 10

Because pointerof involves pointers, it is considered unsafe.