Next: , Previous: , Up: Pointers   [Contents][Index]


mem-aref

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)

Compatibility Note

For compatibility with older versions of CFFI, mem-aref will produce a pointer for the deprecated bare structure specification, but it is consistent with other types for the current specification form (:struct structure-name) and provides a Lisp object translated from the structure (by default a plist). In order to obtain the pointer, you should use the new function mem-aptr.

See Also

mem-ref
mem-aptr