Dear Monks (and especially Tye, who was very kind in helping me on this task earlier):
I am trying to pass a large 2-D float array from Perl to C in order to perform some mathematical functions on it. At the suggestion of Tye, I was told to pack the Perl array and send it through, and then recast a matrix in C. I am not very experienced in C (or much in Perl for that matter, but more so), and thus I'm having some trouble printing the values in the test array I've made. Here is the code I've put together (at the bottom I used the Perl module Inline to put the XSUB together automatically):
#! /usr/bin/perl -w
use strict;
use Inline C;
sub MyFunc(\@);
my $ref1 = [1,2,3,4,5];
my $ref2 = [6,7,8,9,10];
my @matrix;
for (0..$#{$ref1}) {
$matrix[0]->[$_] = ${$ref1}[$_];
}
$_ = 0;
for (0..$#{$ref2}) {
$matrix[1]->[$_] = ${$ref2}[$_];
}
MyFunc(@matrix);
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 getangles(0+@$avMatrix, $width, $packedMatrix);
}
__END__
__C__
int getangles(int rows, int cols, char* packedMatrix) {
int **M = (int **)packedMatrix;
printf("Here I want the first entry of the matrix: %d", M);
}
This seems to return a long number, not the desired number "1." Any suggestions?
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.