Consider a Haskell data-type which looks like this
data MyData = MyData { arrInt :: [Int] , arrDouble :: [Double], arraySize :: N }
Here N represents the sizes of both the arrays the MyData record.
Is it possible to pass this (or some kind of Haskell "pointer" of a MyData object) to a C function which looks like this.
int myfunc (int* a, double* b, int N)
I am able to use the FFI to call C functions accepting and returning simple datat-types like Double, Int, Char etc. But for more complicated types I don't know what to do.
You could do something like that:
import Foreign
import Foreign.C
myfunc :: MyData -> IO CInt
myfunc d =
withArray (map fromIntegral $ arrInt d) $ \arr1 ->
withArray (map CDouble $ arrDouble d) $ \arr2 ->
c_myfunc arr1 arr2 (fromIntegral $ arraySize d)
foreign import ccall safe "myfunc"
c_myfunc :: Ptr CInt -> Ptr CDouble -> CInt -> IO CInt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With