The tricky part of this question is passing arrays of doubles from Perl to C.
, . Perl (AV* C) , . perl , , perlguts
use Inline 'C';
@a = (1,2,3,4,5);
@b = (19,42);
$x = c_function(\@a,\@b);
print "Result: $x\n";
__END__
__C__
#include <stdio.h>
#include <math.h>
double *AV_to_doubleptr(AV *av, int *len)
{
*len = av_len(av) + 1;
double *array = malloc(sizeof(double) * *len);
int i;
for (i=0; i<*len; i++)
array[i] = SvNV( *av_fetch(av, i, 0) );
return array;
}
double the_real_function(const double *x1, int n1, const double *x2, int n2)
{
...
}
double c_function(AV *av1, AV *av2)
{
int n1, n2;
double *x1 = AV_to_doubleptr(av1, &n1);
double *x2 = AV_to_doubleptr(av2, &n2);
double result = the_real_function(x1,n1, x2,n2);
free(x2);
free(x1);
return result;
}