Search code examples
haskellffi

Haskell FFI double pointer argument


I would like to wrap a set of C functions with the following sig.:

ErrorCode Initialize(int *argc,char ***args, ...)

How is the double pointer represented in the FFI call? It is a pointer to a list of strings; is the following plausible?

foreign import ccall unsafe "lib.h Initialize"
    c_initialize :: Ptr CInt 
                 -> Ptr [String]
                 -> IO (Ptr CInt)

Or is the second argument a Ptr (Ptr Char)? I can't find this case in the literature I've read so far (Real World Haskell and the Wikibook) and my C is a bit rusty. Thanks in advance

-- Correction : RWH actually shows the interface to pcre_compile():

-- PCRE-compile.hs
foreign import ccall unsafe "pcre.h pcre_compile"
  c_pcre_compile :: CString
                 -> PCREOption
                 -> Ptr CString
                 -> Ptr CInt
                 -> Ptr Word8
                 -> IO (Ptr PCRE)

Which corresponds to:

-- pcre.h
pcre *pcre_compile(const char *pattern,
               int options,
               const char **errptr,
               int *errofset,
               const unsigned char *tableptr);

Solution

  • So, taking all these suggestions into account, it seems like the most fitting signature is something like

    foreign import ccall unsafe "lib.h Initialize"
    c_initialize :: Ptr CInt 
                 -> Ptr (Ptr CString)
                 -> IO (Ptr CInt)
    

    Thank you all! will keep you posted