PerlXS - Pass an (integer) array to C function (4)

1 Name: dagur : 2008-12-30 12:50 ID:4b47teUm [Del]

Using Array Value (AV) functions, it's possible to access Perl arrays in C. The following code snippet copies integer values from Perl into a typical C int array.

    void
set_site_divider(site_divider)
AV* site_divider
CODE:
I32 l = av_len(site_divider) + 1;
unsigned int *cpy = malloc(sizeof(*cpy) * l);

int i = 0;
for (; i < l; i++) {
SV *site = av_shift(site_divider);
cpy[i] = SvIV(site);
}
set_site_divider(cpy);

av_shift pops the first scalar value (SV) from the array, and SvIV returns the int value stored in the scalar.

The document perlguts contains more functions to manipulate SV's and AV's.

For referance, the C function:

   void set_site_divider(unsigned int *s) {
site_divider = s;
}

And the function call from perl:

   DocInfo::Store::set_site_divider([5, 6, 7, 8, 9]);

2 Name: ikke dagur : 2009-08-26 01:31 ID:UxbKXUja [Del]

:o

3 Name: JA : 2011-09-28 13:00 ID:emyiL0mO [Del]

ABC = ABC
PRINT ABC

4 Name: Anonymous : 2012-04-14 05:25 ID:aE+g8nLG (Image: 320x240 jpg, 17 kb) [Del]

src/1334373916907.jpg: 320x240, 17 kb
Name: Link:
Leave these fields empty (spam trap):
More options...
Verification:
Image: