1

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);
3
  • 2
    Maybe it's a Ptr (Ptr (Ptr CChar)) since we have three * in front of args (and CChar is not Char). Commented Mar 23, 2015 at 0:17
  • 2
    I think the translation from C types to Haskell FFI types is pretty straightforward. char*** is Ptr (Ptr (Ptr CChar)). As far as I know, there is no way to produce or consume a Ptr [String] (other than castPtr) because [String] is not Storable. So having your function take a Ptr [String] would be pretty silly - you can never actually call it from Haskell, unless you have some other FFI function specifically for creating a Ptr [String] for use with c_initialize. Commented Mar 23, 2015 at 4:46
  • 1
    Hint: A char * is usually a CString. Commented Mar 23, 2015 at 7:55

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.