Ah, this makes doing most of the work in Perl even more important (in my book, anyway). So we need to build a bunch of (packed) C arrays and a (packed) array of C pointers to these arrays. That isn't too hard. Here is some code:

sub MyFunc { my $avMatrix= shift(@_); my $packedMatrix= ""; my @recycleBin; my $width= @{ $avMatrix->[0] }; for my $avRow ( @$avMatrix ) { $width= @$avRow if @$avRow < $width; my $packedRow= pack("f$width",@$avRow); push @recycleBin, \$packedRow; $packedMatrix .= pack("p",$packedRow); } return MyFuncC( 0+@$avMatrix, $width, $packedMatrix ); }
and the XS code would look something like this:
int MyFuncC( rows, columns, packedMatrix ) int rows; int columns; char * packedMatrix; CODE: RETVAL= CFunc( rows, columns, (float **)packedMatrix );

Some explanation: A C function that takes a 2-dimensional array (matrix) usually requires that the dimensions of the matrix be passed in. So we need to figure out what to pass in for the width. To prevent the C code from trying to read off the end of the packed arrays that we'll be producing, we just set width to be the minimum width. Another reasonable choice would be to refuse to call the C code if different widths were passed in.

The first pack creates the C (packed) array for a single row of the matrix. We need to prevent the string that contains this packed row from being garbage collected until after the C function is done, so we keep a reference to it alive in @recycleBin until the end of our Perl function.

Then we append a pointer to the start of that packed row to our $packedMatrix, which is just a Perl string that we build up as a packed array of pointers.

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

In reply to (tye)Re3: Passing References to Arrays into Perl Extensions in C by tye
in thread Passing References to Arrays into Perl Extensions in C by Evanovich

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.