[Haskell-cafe] FFI and pointers to pointers

Dimitry Golubovsky golubovsky at gmail.com
Fri Apr 22 11:18:13 EDT 2005


I am trying to generalize my knowledge about FFI declarations when
dealing with pointers to pointers (import from C to Haskell). Maybe
these are silly questions, but It seems to me, I am missing some

Per the FFI Addendum:

For a variable, we use "&" import:

int bar;

foreign import ccall "&" bar :: Ptr CInt

If we have int *pbar;, will Ptr (Ptr CInt) be correct? For arbitrary
level of pointer nesting, will nesting of the same number of Ptr's be

For a function, we use "static" import:

int system (char *str);

foreign import ccall "static stdlib.h" system :: Ptr CChar -> IO CInt

but for a pointer to a function (i. e. a variable) we use:

int (*psystem) (char *str);

foreign import ccall "dynamic"
mkFun :: FunPtr (CInt -> IO ()) -> (CInt -> IO ())

and then psystem should be declared as a variable with "&" import, I
believe. Or as a FunPtr?

And then (mkFun psystem) returns something to be called as a function.

Then (talking to myself) pointer to pointer to a function may be
imported as shown in the very beginning, i. e. by nesting Ptr's?

So indeed my question is: would nesting of Ptr's work if nested
pointers are imported?

I haven't experimented with this yet, but maybe someone has?

Thanks for any advice.

Dimitry Golubovsky

Anywhere on the Web

More information about the Haskell-Cafe mailing list