in reply to Re^2: Perl XS
in thread Perl XS
When I run that script, it compiles, then outputs:use warnings; use strict; use Inline C => Config => BUILD_NOISY => 1; use Inline C => <<'EOC'; void print_array_char(char * array) { int l; int i; l=strlen(array); printf("Length of array is %d\n",l); for(i=0;i < l;i++) { printf("Element array[%d] = %c\n",i,array[i]); } } void print_array_int(int array[], int l) { int i; for(i=0;i < l;i++) { printf("Element array[%d] = %d\n",i,array[i]); } } EOC print_array_char( "revendar" ); print_array_int (-1,12,23,3);
The problem is that, although there's nothing syntactically wrong with print_array_int(), perl doesn't know how to pass the 'int array[]' type to XS.Length of array is 8 Element array[0] = r Element array[1] = e Element array[2] = v Element array[3] = e Element array[4] = n Element array[5] = d Element array[6] = a Element array[7] = r Undefined subroutine &main::print_array_int_alt called at try.pl line +....
"items" is the number of elements on the stack - so there's really no need to pass the length of the array to the function. You could remove that arg and rewrite the for loop condition as (i=0; i<items; i++)void print_array_int(int x, ...) { dXSARGS; int i; for(i=0;i < items - 1; i++) { printf("Element array[%d] = %d\n",i,SvIV(ST(i))); } XSRETURN(0); }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: Perl XS
by davido (Cardinal) on Nov 19, 2013 at 00:26 UTC |