# Post a comment - Liam Healy

## Apr. 28th, 2009

### 09:23 pm - *Foreign structures by value*

I have written Foreign Structures By Value which will allow calling foreign functions that either take or return structures by value instead of by reference (i.e., with a pointer). It requires CFFI and libffi, and should work in any environment where those are both supported.

For example, the complex type is defined in the GNU Scientific Library (GSL) as

typedef struct { double dat[2]; } gsl_complex;

The function `gsl_complex_conjugate`

takes and returns a structure of this type by value,

gsl_complex gsl_complex_conjugate (gsl_complex z);

If we define

(fsbv:defcstruct (complex :constructor complex :deconstructor (realpart imagpart)) (dat :double :count 2)) (defun complex-conjugate (complex-number) (fsbv:with-foreign-objects ((gslin 'complex complex-number)) (object (fsbv:foreign-funcall "gsl_complex_conjugate" complex gslin complex) 'complex)))

then we can call this function from Lisp:

(complex-conjugate #c(3.0d0 4.0d0)) #C(3.0 -4.0)

This is making its way into GSLL but in the meantime should be usable for other projects.