[ocaml-ctypes] string_of_char_ptr

Jeremy Yallop yallop at gmail.com
Sat Jan 20 23:17:07 GMT 2018


On 20 January 2018 at 22:08, john skaller <skaller at internode.on.net> wrote:
> According to Real World Ocaml string_of_char_ptr can be used to convert a
> C char* to an Ocaml string. But I can’t find it anywhere in the API!
>
> Any clues?

I don't think that function is exposed in the API, at least in recent
versions.  But if you have a 'char ptr' value 'p' then you can convert
it to a string using 'coerce':

     coerce (ptr char) string p

For example:

     # let getenv = foreign "getenv" (string @-> returning (ptr char));;
     val getenv : string -> char Ctypes_static.ptr = <fun>
     # let p = getenv "USER";;
     val p : char Ctypes_static.ptr = (char*) 0x7ffe712cd9f8
     # coerce (ptr char) string p;;
     - : string = "jeremy"

Kind regards,

Jeremy


More information about the Ctypes mailing list