# FFI and pointers

**URL:** <https://www.ruby-forum.com/t/ffi-and-pointers/178065>\
**Category:** Ruby\
**Created:** [October 28, 2009, 6:29pm UTC](https://www.ruby-forum.com/t/ffi-and-pointers/178065 "2009-10-28T18:29:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thorsten\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/aca169/32.png) [@Thorsten\_H](https://www.ruby-forum.com/u/Thorsten_H)\
**Post date:** [October 28, 2009, 6:29pm UTC](https://www.ruby-forum.com/t/ffi-and-pointers/178065/1 "2009-10-28T18:29:55Z")

</div>

Hello,

I’m playing around with FFI and encountered a problem, I could not solve  
till now.  
Let’s assume I use standard C’s malloc in my wrapper:

attach\_function :malloc, [:int], :pointer

so far no problem. But what now? The pointer returned is of type void\*,  
which is of no help, since I need double\*, int\* and float\* …  
Is there a type cast mechanism in FFI (or anything similar) or do I have  
to wrap  
the original library in another layer of C (alternatively write a helper  
lib…)  
Or can I import C’s typecasts using FFI itself?

Best regards,  
Thorsten

---

<div class="post-metadata">

**Author:** ![Thorsten\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/aca169/32.png) [@Thorsten\_H](https://www.ruby-forum.com/u/Thorsten_H)\
**Post date:** [October 28, 2009, 8:58pm UTC](https://www.ruby-forum.com/t/ffi-and-pointers/178065/2 "2009-10-28T20:58:36Z")

</div>

Thorsten H. wrote:

> Is there a type cast mechanism in FFI (or anything similar) or do I  
> have to wrap  
> the original library in another layer of C (alternatively write a  
> helper lib…)  
> Or can I import C’s typecasts using FFI itself?
> 
> Best regards,  
> Thorsten

If nothing else, you can use the read\_\* and write\_\* methods (like  
read\_int) to interact with it.

-Justin

---

<div class="post-metadata">

**Author:** ![Thorsten\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/aca169/32.png) [@Thorsten\_H](https://www.ruby-forum.com/u/Thorsten_H)\
**Post date:** [October 28, 2009, 10:05pm UTC](https://www.ruby-forum.com/t/ffi-and-pointers/178065/3 "2009-10-28T22:05:20Z")

</div>

Well, the problem is not to use the pointer type as such, but  
compatibility issue.  
I have this function behaving like malloc, and other functions  
which take pointers to - let’s say - float.  
Since malloc returns void\* I suspect the problems I experience  
are due to this mismatch.

Justin C. schrieb:
