double do_nothing(double x[], int sizeOfX)

My advice is to install Inline::C and try this script:
use strict; use warnings; use Math::BigInt; use Inline C => Config => BUILD_NOISY => 1, USING => 'ParseRegExp', CLEAN_AFTER_BUILD => 0, ; use Inline C => <<'EOC'; double do_nothing(SV * x, ...) { dXSARGS; /* enable stack manipulation */ int index; for(index=0; index<items; index++) { printf("%f ", SvNV(ST(index))); ST(index)=newSVnv(1.34); } printf("\n"); return SvNV(ST(0)); } EOC my @in = (2.3, 3.4, 4.5, 6.8); my $d = do_nothing(@in); print $d, "\n"; my @next = (-1.7, -1.3, @in); my $d2 = do_nothing(@next); print $d2, "\n"; __END__ Outputs: 2.300000 3.400000 4.500000 6.800000 1.34 -1.700000 -1.300000 2.300000 3.400000 4.500000 6.800000 1.34
Because that script specifies CLEAN_AFTER_BUILD=>0 you can then cd into the ./_Inline/build directory and view the XS file that was autogenerated and used.
(I've never really learned how to write XS - I just use the XS file that Inline::C creates.)

There are of course, other ways to formulate do_nothing() - have a look at perldoc Inline::C-Cookbook for examples of the above and other perl API usage.
And see perldoc perlapi for documentation on the API functions.

In XS, it is allowable to pass a list of "double" types as arguments, but I think passing a variable-sized list of doubles (as you intended) would turn out to be messier than the above demo.
And that's why I've opted to pass the values as a list of "SV*" types.

You could also write do_nothing() as:
double do_nothing(SV * x) { ... do stuff }
In that case you'd be calling do_nothing() from perl as something like:
my $double = do_nothing(\@in);
I can give you a demo of that, too, if you like.

As a means to understanding the perl API, I really can't recommend Inline::C strongly enough.
It gives one the opportunity to quickly and easily trial constructs about which one is unsure - and is, IMO, the best available XS/API learning tool.

Cheers,
Rob

In reply to Re: perl XS - passing array to C and getting it back by syphilis
in thread perl XS - passing array to C and getting it back by kopolov

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.