Previous: , Up: Alien Types   [Contents][Index]


8.2.4 The C-Call Package

The c-call package exports these type-equivalents to the C type of the same name: char, short, int, long, signed-char, unsigned-char, unsigned-short, unsigned-int, unsigned-long, float, double. Note that char and signed-char are the same type, i.e, both are signed characters. c-call also exports these types:

Alien type: void

This type is used in function types to declare that no useful value is returned. Evaluation of an alien-funcall form will return zero values.

Alien type: c-string

This type is similar to (* char), but is interpreted as a null-terminated string, and is automatically converted into a Lisp string when accessed. If the pointer is C NULL (or 0), then accessing gives Lisp nil.

With Unicode, a Lisp string is not the same as a C string since a Lisp string uses two bytes for each character. In this case, a C string is converted to a Lisp string by taking each byte of the C-string and applying code-char to create each character of the Lisp string.

Similarly, a Lisp string is converted to a C string by taking the low 8 bits of the char-code of each character and assigning that to each byte of the C string.

In either case, string-encode and string-decode may be useful to convert Unicode Lisp strings to or from C strings.

Assigning a Lisp string to a c-string structure field or variable stores the contents of the string to the memory already pointed to by that variable. When an Alien of type (* char) is assigned to a c-string, then the c-string pointer is assigned to. This allows c-string pointers to be initialized. For example:

  (def-alien-type nil (struct foo (str c-string)))
  
  (defun make-foo (str)
    (let ((my-foo (make-alien (struct foo))))
      (setf (slot my-foo 'str) (make-alien char (length str)))
      (setf (slot my-foo 'str) str)
      my-foo))

Storing Lisp nil writes C NULL to the c-string pointer.


Previous: Alien Type Specifiers, Up: Alien Types   [Contents][Index]