Link

Pointers to C Objects

Pointers to C objects can be retrieved with a function similar to Foreign.foreign_value:

extern char **environ; /* see `man 7 environ` */
let () =
  let environ = [%c foreign_value "environ" (ptr string_opt)] in
  let rec iter env =
    match !@env with
    | None -> ();
    | Some s ->
      print_endline s;
      iter (env +@ 1)
  in
  iter !@environ

This feature can also be used to import const qualified objects or double literals at runtime:

let%c () = header {|
#include <math.h>
static const double m_2_sqrtpi = M_2_SQRTPI;
|}

let _M_2_SQRTPI = !@ [%c foreign_value "m_2_sqrtpi" double]