Next: , Previous: make-pointer, Up: Pointers


mem-aref

Syntax

— Accessor: mem-aref ptr type &optional (index 0)

(setf (mem-aref ptr type &optional (index 0)) new-value)

Arguments and Values

ptr
A foreign pointer.
type
A foreign type.
index
An integer.
new-value
A Lisp value compatible with type.

Description

The mem-aref function is similar to mem-ref but will automatically calculate the offset from an index.

  (mem-aref ptr type n)
   
  ;; is identical to:
   
  (mem-ref ptr type (* n (foreign-type-size type)))

Examples

  CFFI> (with-foreign-string (str "Hello, foreign world!")
          (mem-aref str :char 6))
  => 32
  CFFI> (code-char *)
  => #\Space
   
  CFFI> (with-foreign-object (array :int 10)
          (loop for i below 10
                do (setf (mem-aref array :int i) (random 100)))
          (loop for i below 10 collect (mem-aref array :int i)))
  => (22 7 22 52 69 1 46 93 90 65)

See Also

mem-ref