From: Sandeep K. <san...@gm...> - 2006-04-25 15:19:15
|
Hi, You must have figured out how to get the values from an array. I used this to retrieve the values from a C array. sub get_array{ $len =3D $_[0]; $ia =3D $_[1]; @arr[$len]; for($i =3D 0;$i < $len;$i++){ $arr[$i] =3D example::int_get($ia,$i); } return @arr; } Nothing needs to be added in the swig interface file. int_get is already provided as you have mentioned. Thanks Sandeep On 4/24/06, Albert Vernon Smith <av...@gm...> wrote: > > Hi Sandeep-- > > I am trying to do the same as you, and have figured out how to pass > arrays from Perl to C, but still need to figure out the reverse. For > starters, I have largely been following the page <http://www.swig.org/ > papers/Perl98/swigperl.htm> to get to this point. It covers an older > version of SWIG, though still has been very informative and helpful. > > First, I want to call the following test function: > > int func10(int mat[], int *len){ > register int i; > int tmp; > tmp =3D *len; > > for(i=3D0; i < tmp; i++) > { > printf("%5.2d\n", mat[i]); > } > return; > } > > I have defined the following typemap and helper functions in my > interface file: > > %include typemaps.i > > %apply int *INPUT { int *len }; > > %inline %{ > int *int_array(int size) { > return (int *) malloc(sizeof(int)*(4+size)); > } > void int_destroy(int *a) { > free(a); > } > void int_set(int *a, int i, int val) { > a[i] =3D val; > } > int int_get(int *a, int i) { > return a[i]; > } > %} > > Then, in perl, I have the following: > > sub create_array { > my $len =3D scalar(@_); > my $ia =3D example::int_array($len); > for (my $i =3D 0; $i < $len; $i++) { > my $val =3D shift; > example::int_set($ia,$i,$val); > } > return $ia; > } > > And I call everything as: > > my @a =3D (1..12); > my $ia =3D create_array(@a); > example::func10($ia, scalar(@a)); > > This has worked fine for me. (In reality, one also will need to > destroy the arrays after one is done, and see the document I cited > above on how to do that.) You can use the same to also call is a > function written as: > > int func11(int *mat, int *len) > { > register int i; > int tmp; > tmp =3D *len; > > for(i =3D 0 ; i < tmp ; i++) > { > printf("%5.2d\n", *(mat + i)); > } > printf("\n"); > return; > } > > I think this should help solve part of what you want to do, but not > how to get the results back to Perl, which I am also trying to figure > out. I'd certainly appreciate any insight anyone can give on that > point. > > -albert > > On 24.4.2006, at 14:43, Sandeep Kumar wrote: > > > Hi, > > > > I have a library written in C and am trying to make a SWIG > > interface to make it accessible in perl. > > Am able to work with simple datatypes and structures. > > But got stuck with arrays with types int*, char* and void * which > > are being passed as arguments, returned as function values and are > > present as member of struct. > > > > What I need is some direction, indicating which section of the SWIG > > documentation I should look at for solution of this. > > And if any examples available for this, that would be great. > > > > Thanks > > Sandeep Kumar > > |