# C FFI - Can I get a function pointer from a value - not using ctypes

**URL:** <https://discuss.ocaml.org/t/c-ffi-can-i-get-a-function-pointer-from-a-value-not-using-ctypes/1509>\
**Category:** Learning\
**Tags:** ffi\
**Created:** [January 31, 2018, 12:00am UTC](https://discuss.ocaml.org/t/c-ffi-can-i-get-a-function-pointer-from-a-value-not-using-ctypes/1509 "2018-01-31T00:00:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sagra](https://avatars.discourse-cdn.com/v4/letter/s/f4b2a3/32.png) [@sagra](https://discuss.ocaml.org/u/sagra)\
**Post date:** [January 31, 2018, 12:00am UTC](https://discuss.ocaml.org/t/c-ffi-can-i-get-a-function-pointer-from-a-value-not-using-ctypes/1509/1 "2018-01-31T00:00:14Z")

</div>

I’m trying to wrap up some C that makes extensive use of function pointers. How can I get a function pointer out of an ocaml value? I looked at how ctypes generated the stubs for something like this, but there was a mysterious funptr(arg) in the resulting C that I couldn’t find the definition of.

What I’m after is:

```
typedef int (*int_to_int)(int input);

int_to_int foo(value ocaml_fn) {
    // assuming ocaml_fn is of type int -> int
    // some code here to get a function pointer
    return(funptr);
}

```

Thanks

---

<div class="post-metadata">

**Author:** ![blez](https://avatars.discourse-cdn.com/v4/letter/b/898d66/32.png) [@blez](https://discuss.ocaml.org/u/blez)\
**Post date:** [February 2, 2018, 10:02am UTC](https://discuss.ocaml.org/t/c-ffi-can-i-get-a-function-pointer-from-a-value-not-using-ctypes/1509/2 "2018-02-02T10:02:59Z")

</div>

The way I normally store pointers in OCaml values is in a string. So you would allocate the memory using something like `ml_p = caml_alloc_string(sizeof int_to_int)` and copy the the pointer into this with `memmove(String_val(ml_p), (char *)fp, sizeof int_to_int)`

Getting the data back out will just be the reverse: `memmove((char *)fp, String_val(ml_p), sizeof int_to_int)`.

However, it really depends on how the function pointer was stored in the OCaml value.

---

<div class="post-metadata">

**Author:** ![dra27](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dra27/32/5294_2.png) [@dra27](https://discuss.ocaml.org/u/dra27)\
**Post date:** [February 2, 2018, 10:17am UTC](https://discuss.ocaml.org/t/c-ffi-can-i-get-a-function-pointer-from-a-value-not-using-ctypes/1509/3 "2018-02-02T10:17:02Z")

</div>

@blez - I think you’ve misunderstood the question, which is how to obtain a C function pointer to an OCaml function, not how to store a C function pointer in an OCaml value.

---

<div class="post-metadata">

**Author:** ![dra27](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dra27/32/5294_2.png) [@dra27](https://discuss.ocaml.org/u/dra27)\
**Post date:** [February 2, 2018, 10:18am UTC](https://discuss.ocaml.org/t/c-ffi-can-i-get-a-function-pointer-from-a-value-not-using-ctypes/1509/4 "2018-02-02T10:18:24Z")

</div>

@blez - as an aside, it would be better to be using custom blocks, as ~~God~~ Damien intended, rather than (ab)using strings!

---

<div class="post-metadata">

**Author:** ![blez](https://avatars.discourse-cdn.com/v4/letter/b/898d66/32.png) [@blez](https://discuss.ocaml.org/u/blez)\
**Post date:** [February 2, 2018, 10:46am UTC](https://discuss.ocaml.org/t/c-ffi-can-i-get-a-function-pointer-from-a-value-not-using-ctypes/1509/5 "2018-02-02T10:46:00Z")

</div>

@dra27 - I’ve seen both used for simple pointers (ones that don’t need to be free’d before the GC) but agree custom blocks are generally better.

Sorry for the confusion on the callbacks.

---

<div class="post-metadata">

**Author:** ![blez](https://avatars.discourse-cdn.com/v4/letter/b/898d66/32.png) [@blez](https://discuss.ocaml.org/u/blez)\
**Post date:** [February 2, 2018, 7:27pm UTC](https://discuss.ocaml.org/t/c-ffi-can-i-get-a-function-pointer-from-a-value-not-using-ctypes/1509/6 "2018-02-02T19:27:51Z")

</div>

@sagra: I had a look at this as I haven’t done this before (full disclosure!) but I think it should be doable if you combine the C library [FFCALL](https://www.haible.de/bruno/packages-ffcall.html) (or [GNU’s version](https://www.gnu.org/software/libffcall/)) with the OCaml [callback](https://caml.inria.fr/pub/docs/manual-ocaml/intfc.html#sec448) API. You would generate a new function with the OCaml closure as a parameter and pass this as the callback function. This will also need [custom blocks](https://caml.inria.fr/pub/docs/manual-ocaml/intfc.html#sec459) as you will need to free memory when a callback goes out of scope

Depending on your library API the functions may also have a callback parameter (usually void \*) that get’s passed to the callback blindly, in which case you can put the closure value ih this.

I hope this helps.
