Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a C Pointer in MacRuby?

I am working with a C Framework in MacRuby which has a structure

int CPhidget_getSerialNumber(CPhidgetHandle phid,int * serialNumber)    

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

In MacRuby I have tried the following:

i = CPhidget_getSerialNumber(@encoder, nil)

The above gets me a number but not the serial number.

CPhidget_getSerialNumber(@encoder, i)

This gets me an error stating: expected instance of Pointer, got `0' (Fixnum) (TypeError) (which doesn't surprise me).

So my main question is: Is there an equivalent of a C Pointer in MacRuby?

like image 279
tsugua Avatar asked Dec 07 '25 10:12

tsugua


1 Answers

MacRuby has a Pointer class, which can stand in for a C pointer. So, you could 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 Ruby's array access syntax:

i[0] #=> value from CPhidget_getSerialNumber
like image 79
Josh Avatar answered Dec 09 '25 00:12

Josh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!