# Casting a Ctype pointer to an abstract type

**URL:** <https://discuss.ocaml.org/t/casting-a-ctype-pointer-to-an-abstract-type/5491>\
**Category:** Learning\
**Tags:** ctypes, ffi\
**Created:** [April 11, 2020, 6:33pm UTC](https://discuss.ocaml.org/t/casting-a-ctype-pointer-to-an-abstract-type/5491 "2020-04-11T18:33:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ifazk](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ifazk/32/836_2.png) [@ifazk](https://discuss.ocaml.org/u/ifazk)\
**Post date:** [April 11, 2020, 6:33pm UTC](https://discuss.ocaml.org/t/casting-a-ctype-pointer-to-an-abstract-type/5491/1 "2020-04-11T18:33:51Z")

</div>

I have two libraries, one which uses `Ctypes` and another which does FFI directly and I would like to pass values between them.

```ocaml
module Ct = struct
  type t = unit ptr
  let foo : unit -> t = ...
end

module Fi = struct
   type t
  external bar : t -> ... = ...
end

```

The underlying data structures managed by both libraries are the same. How can I cast a `Ct.t` to a `Fi.t`?

---

<div class="post-metadata">

**Author:** ![ygrek](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ygrek/32/268_2.png) [@ygrek](https://discuss.ocaml.org/u/ygrek)\
**Post date:** [April 13, 2020, 7:04pm UTC](https://discuss.ocaml.org/t/casting-a-ctype-pointer-to-an-abstract-type/5491/2 "2020-04-13T19:04:58Z")

</div>

> The underlying data structures managed by both libraries are the same.

there is nothing on the ocaml side to prove that, so it seems in the end you will need smth like `external cast : nativeint -> Fi.t` and `Ctypes.raw_address_of_ptr : unit ptr -> nativeint`

---

<div class="post-metadata">

**Author:** ![ifazk](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ifazk/32/836_2.png) [@ifazk](https://discuss.ocaml.org/u/ifazk)\
**Post date:** [April 13, 2020, 9:53pm UTC](https://discuss.ocaml.org/t/casting-a-ctype-pointer-to-an-abstract-type/5491/3 "2020-04-13T21:53:10Z")

</div>

I was trying to avoid having to write a manual cast function in C.

But it looks like I can abuse the `Ctypes.Root.get` function for this.
