Next: , Previous: Allocating Foreign Memory, Up: Pointers

7.3 Accessing Foreign Memory

When manipulating raw C data, consider that all pointers are pointing to an array. When you only want one C value, such as a single struct, this array only has one such value. It is worthwhile to remember that everything is an array, though, because this is also the semantic that C imposes natively.

C values are accessed as the setf-able places defined by mem-aref and mem-ref. Given a pointer and a CFFI type (see Foreign Types), either of these will dereference the pointer, translate the C data there back to Lisp, and return the result of said translation, performing the reverse operation when setf-ing. To decide which one to use, consider whether you would use the array index operator [n] or the pointer dereference * in C; use mem-aref for array indexing and mem-ref for pointer dereferencing.