Next: , Previous: Optimizing Type Translators, Up: Foreign Types


5.6 Foreign Structure Types

For more involved C types than simple aliases to built-in types, such as you can make with defctype, CFFI allows declaration of structures and unions with defcstruct and defcunion.

For example, consider this fictional C structure declaration holding some personal information:

  struct person {
    int number;
    char* reason;
  };

The equivalent defcstruct form follows:

  (defcstruct person
    (number :int)
    (reason :string))

CFFI knows how to align C structs, and how to figure in padding between struct elements.

Please note that this interface is only for those that must know about the values contained in a relevant struct. If the library you are interfacing returns an opaque pointer that needs only be passed to other C library functions, by all means just use :pointer or a type-safe definition munged together with defctype and type translation.