Hi,

Courtesy of a typemap, header file and c file I can pass references to
integer arrays from perl to c.

use Inline C => <<'END_OF_C_CODE';

#include "arrays.h" /* from Math::FFT */
#include "arrays.c" /* from Math::FFT */

/* Also place a copy of 'typemap'
   from MAth::FFT in cwd */

void s_add (int len, int *a, int *b) {
int i;
for (i = 0; i < len; ++i) {
a[i] += b[i];
}

}

END_OF_C_CODE

@one = (10,11,12,13,14);
@two = (21,23,25,51,99);

s_add(scalar(@one), \@one, \@two);

Sadly, however, the result of the action performed by s_add does not get
passed back to perl's '@one' (which still contains the original values).

To get the results back to perl, I can use the Inline_Stack_Vars macros to
push the values in the C array 'a' onto the perl stack, and rewrite the perl
code as:
@one = s_add(scalar(@one), \@one, \@two);
I think I've also seen some code for passing back references. In any event,
as soon as I make those changes to the s_add code, the function cannot be
called from C.

Given that I wish to be able to use s_add as both a perl function and a C
function, is it best therefore, to write 2 versions of the function - one
which returns to perl and another which could be used by C ?

(Incidentally, I've found that in order to call s_add from the C code I have
to delete 'void' from the definition .... took 3 hours of staring to realise
that.)

Cheers,
Rob


Reply via email to