# Ctypes passing string pointer or null pointer

**URL:** <https://discuss.ocaml.org/t/ctypes-passing-string-pointer-or-null-pointer/4245>\
**Category:** Learning\
**Tags:** ctypes\
**Created:** [August 14, 2019, 8:17am UTC](https://discuss.ocaml.org/t/ctypes-passing-string-pointer-or-null-pointer/4245 "2019-08-14T08:17:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gabrik](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/gabrik/32/876_2.png) [@gabrik](https://discuss.ocaml.org/u/gabrik)\
**Post date:** [August 14, 2019, 8:17am UTC](https://discuss.ocaml.org/t/ctypes-passing-string-pointer-or-null-pointer/4245/1 "2019-08-14T08:17:34Z")

</div>

Hi all,

I’m trying to wrap some basic Linux syscall and sometimes I have case in which I want to pass a pointer to a string or a null pointer.

Let’s take [mount(2)](http://man7.org/linux/man-pages/man2/mount.2.html) as an example, I may want to pass NULL as 3rd parameter, so in OCaml

```
  let _mount =
  foreign "mount" (ptr char @-> ptr char @-> ptr_opt char @-> int @-> ptr void @-> returning int)

  let mount source target fstype oflags =
    let flags = 0 in
    let flags = List.fold_left (fun (flags:int) (e:flag) ->
      flags lor (flag_to_int e)
    ) flags oflags
    in
    let csource = CArray.of_string source in
    let ctarget = CArray.of_string target in
    let cfstype =
      match fstype with
      | Some fs -> Some (CArray.of_string fs)
      | None -> None
    in
    _mount csource ctarget cfstype flags (Ctypes.null)

```

The problem is that offcourse I get an error on the types when I try to build

```
Error: This expression has type char carray
       but an expression was expected of type
         char Ctypes_static.ptr = (char, [`C]) pointer

```

I have also tried by using Ctypes.null in the pattern matching, but offcourse I get the same error  
Any suggestion?

Thanks,  
Gabriele

---

<div class="post-metadata">

**Author:** ![gabrik](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/gabrik/32/876_2.png) [@gabrik](https://discuss.ocaml.org/u/gabrik)\
**Post date:** [August 14, 2019, 8:58am UTC](https://discuss.ocaml.org/t/ctypes-passing-string-pointer-or-null-pointer/4245/2 "2019-08-14T08:58:51Z")

</div>

Just solved, I should use CArray.start to get the actual pointer to the string  
So the code becomes like this:

```
let _mount =
foreign "mount" (ptr char @-> ptr char @-> ptr_opt char @-> int @-> ptr void @-> returning int)

  let mount source target fstype oflags =
    let flags = 0 in
    let flags = List.fold_left (fun (flags:int) (e:flag) ->
      flags lor (flag_to_int e)
    ) flags oflags
    in
    let csource = CArray.start (CArray.of_string source) in
    let ctarget = CArray.start (CArray.of_string target) in
    let cfstype =
      match fstype with
      | Some fs -> Some (CArray.start (CArray.of_string fs))
      | None -> None
    in
    _mount csource ctarget cfstype flags (Ctypes.null)
```

---

<div class="post-metadata">

**Author:** ![osener](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/osener/32/1621_2.png) [@osener](https://discuss.ocaml.org/u/osener)\
**Post date:** [August 14, 2019, 9:02am UTC](https://discuss.ocaml.org/t/ctypes-passing-string-pointer-or-null-pointer/4245/3 "2019-08-14T09:02:33Z")

</div>

You can use [`string_opt`](https://docs.mirage.io/ctypes/Ctypes_types/module-type-TYPE/index.html#val-string_opt) to let Ctypes do conversion from/to `string option` for you.

Another option is to use [`coerce`](https://docs.mirage.io/ctypes/Ctypes/index.html#val-coerce):

```ocaml
Ctypes.(coerce (ptr void) (ptr char) null)

```

---

<div class="post-metadata">

**Author:** ![gabrik](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/gabrik/32/876_2.png) [@gabrik](https://discuss.ocaml.org/u/gabrik)\
**Post date:** [August 14, 2019, 9:05am UTC](https://discuss.ocaml.org/t/ctypes-passing-string-pointer-or-null-pointer/4245/4 "2019-08-14T09:05:37Z")

</div>

Thanks @osener indeed I will switch to string\_opt also because the signature of the C is using `const char *` it will not need to modify/write to the string so it seems more clean to pass them in that way

Thanks!!
