[Haskell-cafe] Re: FFI"pointer" data types question
Ben Franksen
ben.franksen at online.de
Fri Dec 28 10:57:35 EST 2007
Galchin Vasili wrote:
> If I am calling a ANSI function that requires a pointer to a C
> struct,
> which FFI pointer type should use?
A commonly used technique is to declare an empty data type
data MyStruct
and then use
Ptr MyStruct
in the FFI call. This is useful if the C struct is opaque, or at least meant
to be opaque (i.e. you are not allowed to access its elements directly).
For non-opaque structs, use a Haskell record and make it an instance of
class Storable . An example:
struct timeval
{
__time_t tv_sec; /* Seconds. */
__suseconds_t tv_usec; /* Microseconds. */
};
--->
data Timeval = Timeval {
tv_sec :: TimeT,
tv_usec :: USecondsT
}
(with suitable definitions of TimeT and USecondsT)
instance Storable Timeval where
...
and your FFI calls will get a Ptr Timeval as argument.
HTH
Ben
More information about the Haskell-Cafe
mailing list