From: Kouhei Sutou Date: 2006-11-15T21:40:52+09:00 Subject: Re: Ruby <-SWIG-> C arrays Hi, In <2250e499a84aaf577be46b5e5ffd238a@ruby-forum.com> "Ruby <-SWIG-> C arrays" on Wed, 15 Nov 2006 02:04:14 +0900, Aureliano Buendia wrote: > > typedef unsigned long uInt32 > someFunction(uInt32 someArray[]) > > The array is simply some input. Calling the wrapped function in Ruby: > > Wrapper.someFunction([1, 2, 3]) > > produces this error: Here is a my answer: %module "Example"; %inline %{ typedef unsigned long uInt32; %} %typemap(in, numinputs=1) (uInt32 some_array[], int length) { int i; $2 = RARRAY($input)->len; $1 = ALLOCA_N(uInt32, $2); for (i = 0; i < $2; i++) { $1[i] = NUM2INT(RARRAY($input)->ptr[i]); } }; %inline %{ uInt32 some_function(uInt32 some_array[], int length) { int i; uInt32 sum = 0; for (i = 0; i < length; i++) { sum += some_array[i]; } return sum; } %} Compile: % swig -ruby a.i && ruby -r mkmf -e 'create_makefile("Example")' && make Test: % ruby -r Example -e 'p Example.some_function([1, 2, 3])' 6 Thanks, -- kou