How to create a C-pointer in MacRuby?

I am working with the C Framework on MacRuby which has a structure

int CPhidget_getSerialNumber(CPhidgetHandle phid,int * serialNumber)    

You can see the full documentation here (if you are interested).

On MacRuby, I tried the following:

i = CPhidget_getSerialNumber(@encoder, nil)

The above number is me, not the serial number.

CPhidget_getSerialNumber(@encoder, i)

This causes me an error: the expected pointer instance received `0 '(Fixnum) (TypeError) (which does not surprise me).

So my main question is: is there a C-pointer equivalent in MacRuby?

+3
source share
1 answer

MacRuby has a Pointer class that can stand for a C pointer. So, you can do something like:

i = Pointer.new(:int)
CPhidget_getSerialNumber(@encoder, i)

Then, to extract the results from the pointer (i.e. dereference it), you use the Ruby array access syntax:

i[0] #=> value from CPhidget_getSerialNumber
+2
source

All Articles