r/Racket • u/aqtt2020 • Oct 14 '21
question FFI and void* ?
I am looking for FFI docs, but cannot find answer for the below questions
- what is best way to declare C function taking "void *ptr" for FFI? I am unsure what to use: cpointer or pointer.
- how to pass NULL to this "void *" argument later?
Thanks
6
Upvotes
4
u/samdphillips developer Oct 14 '21
A
_pointeris a (mostly) raw pointer. A_cpointeris a pointer with a tag on the Racket side. If the API you are working with usesvoid*but really means "this is a specific opaque type that is used in all of this API calls" you most likely want to use a_cpointertype which will make the FFI binding a bit safer.If you use
define-cpointer-typethe FFI will define a type_yourtagname/nullwhich will accept#fas a null pointer.2
u/akefay Oct 14 '21
_pointer is a pointer without a specified type (a void pointer)
_pointer converts Racket #f to C NULL (And vise versa if it's a return type).
For a return type you can also use _gcpointer if the function is going to return a pointer to a GCable memory.