in reply to Passing References to Arrays into Perl Extensions in C

Make your life simple:

sub MyFunc { my $arrRef= shift(@_); my $packedArr= pack("f*",@$arrRef); return MyFuncC( 0+@$arrRef, $packedArr ); }
write in Perl what is easy to do in Perl and write in C only what is easy to write in C. Then you can declare your XS code like:
int MyFuncC( count, packedArray ) int count; char * packedArray; CODE: RETVAL= CFunc( count, (float *)packedArray );
Note that this changes depending on the data type of the array to be used by the C function. Provide more details if you have questions.

Updated to work for floats.

        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
Re: (tye)Re: Passing References to Arrays into Perl Extensions in C
by Evanovich (Scribe) on Aug 03, 2001 at 19:23 UTC
    Hi tye--- Thanks for your response. My reference is to a matrix of float values. Will char * still work? I don't think so.... I guess I should have made that clear in my question.

      Yes, "char *" will work fine. I updated my previous node to work with floats. The "char *" tells XS that you want a pointer to the string value of the scalar that was passed in. The pack produces a string that contains a C array of floats, so the address of that can just be typecasted to get a pointer to an array of floats.

              - tye (but my friends call me "Tye")
        Sorry to belabor this point, tye--I'm not very familiar with XS, and I want to make sure I do this right. So if I have a reference to a two-dimensional array, and I just pack it and send it through to C, then can I get, inside the C code, the double pointer to the two-dimensional array of floats? I still just don't see how I work with a char * when doing the mathematical manipulations on the matrix that i need to do. Sorry again.