Hello-- I'm writing a perl extension in C. I want to send an array from the C function back to the main Perl section of the code. I've tried recasting my float array to a char * string, but i seem unable to get the packed array back out to Perl. Here is the code i have thus far (this is the XSUB function, as I don't think the perl code is the problem at this point, since the packed array seems to contain nothing:
MODULE = f PACKAGE = f #include "math.h" #define PI 3.1459 char * f(rows, cols, packedmatrix) int rows int cols char * packedmatrix CODE: float **M; float len1, len2, dotproduct; int k1; int k2; int k3; float theta; float product; char * packedangles; float angles[cols][cols]; M = (float**)packedmatrix; for (k1 = 0; k1<(cols-1); ++k1) { for (k2 = (k1+1); k2<(cols); ++k2) { dotproduct = 0; len1 = 0; len2 = 0; for (k3 = 0; k3 < rows; ++k3) { if ((M[k3][k1] != 999) && (M[k3][k2] != 999)) { dotproduct = dotproduct + (M[k3][k1])*(M[k3][k2]); len1 = len1 + pow((M[k3][k1]), 2); len2 = len2 + pow((M[k3][k2]), 2); } } theta = 180*(acos(dotproduct/sqrt(len1*len2)))/PI; angles[k1][k2]=theta; printf("The angle between columns %d and columns %d is %f\n\n", k +1, k2, theta); } packedangles = (char*)angles; printf("Packed angles: %s", packedangles); RETVAL = packedangles; } OUTPUT: RETVAL
Any help would be most most appreciated. Thanks! Evan

In reply to Sending packed data from C to Perl 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.