[Haskell-cafe] ffi and pass by value structs

Federico Squartini federico.squartini at googlemail.com
Wed Jan 30 08:16:01 EST 2008

Is there a way in the foreign function interface to pass to a function
a struct by value? I explain it better.

There are two ways in c to pass structs, as in the following example:

/* begin c code */

struct couple{
  int a;
  int b;

//by value
int print_couple (struct couple cpl)
  printf("%d\n", cpl.a);
  printf("%d\n", cpl.b);

//by address
int print_couple_ptr (struct couple *cpl)
  printf("%d\n", cpl->a);
  printf("%d\n", cpl->b);

/* end c code */

Apparently in the ffi it is possible to import the second version, as:

foreign import ccall "print_couple_ptr" printCouple::Ptr Couple->IO Int

But not the first, as a struct it's not an atomic type.
So how does one solve this issue?



More information about the Haskell-Cafe mailing list