Search code examples
haskellffic2hs

How do i pass NULL to the foreign function with argument of type char*?


I'm writing Haskell bindings to some C project and there is a function of type

void foo(char *);

The problem is that foo checks this pointer for NULL value and do something different from normal behavior. In my Haskell source wrapper for this function have type foo :: String -> IO () and using newCString inside to marshal it's argument.

I wonder how do i give user ability pass NULL there? I've been expecting that newCString "" would give me 0 since "" /= "\0", but that's not the case.

The only way i see for now is to use "" as indicator that user wants NULL, but that seems hackish. I'm expecting that this problem is quite common, but didn't found a question on SO.


Solution

  • You could change your function to

    foo :: Maybe String -> IO ()
    

    And then for Nothing send a nullPtr to your C function.