I'm not an Inline expert, but here are some examples I have from previous experimenting. The thing you need to consider is the type of data in the arrays, Perl takes care of it for you automatically, but in C, you need to be exact.
#!/usr/bin/perl use warnings; use strict; use Inline C=>'DATA'; my @array = ( 1 .. 100 ); print get_an_element(\@array)."\n"; __DATA__ __C__ SV* get_an_element(SV* aref) { AV* array; if (! SvROK(aref)) croak("aref is not a reference"); array = (AV*)SvRV(aref); SV** v = av_fetch(array,8, 0); if( v == NULL ) croak("not enough elements in array"); SV* ret = *v; SvREFCNT_inc(ret); return ret; } __END__
#!/usr/bin/perl use warnings; use Tk; use Inline C; use strict; $| = 1; my $mw = tkinit; $mw->Button( -text => 'Get Array from C', -command => sub { &get_data(); })->pack(); $mw->Button( -text => 'Quit', -command => sub { exit; })->pack(); MainLoop; ######################################## sub get_data { my $freq_adj = .1; my $vol = .5; my @raw = gen($freq_adj, $vol); #print "@raw\n\n"; foreach(@raw){ print $_,"\n"; } } __END__ __C__ #include <stdio.h> #include <math.h> /* function must return void for Inline to use the stack See the Inline_C cookbook */ void gen(float freq_inc , float vol) { /* two numbers to work with */ float result, rad; rad = .1; Inline_Stack_Vars; Inline_Stack_Reset; while ( rad < 6.3 ) { rad = rad + freq_inc; result = vol * 32768 * sin(rad); Inline_Stack_Push(sv_2mortal(newSVnv(result))); /* newSVnv for floats, newSViv for ints, see perldoc perlguts * +/ } Inline_Stack_Done; }
#!/usr/bin/perl use warnings; use Tk; use Inline C; use strict; $| = 1; #this ones returns ints, not floats my $mw = tkinit; $mw->Button( -text => 'Get Array from C', -command => sub { &get_data(); })->pack(); $mw->Button( -text => 'Quit', -command => sub { exit; })->pack(); MainLoop; ###################################### sub get_data { my $freq_adj = .1; my $vol = .5; my @raw = gen($freq_adj, $vol); print "@raw\n\n"; } __END__ __C__ #include <stdio.h> #include <math.h> /* function must return void for Inline to use the stack See the Inline_C cookbook */ void gen(float freq_inc , float vol) { /* two numbers to work with */ float result, rad; rad = .1; Inline_Stack_Vars; Inline_Stack_Reset; while ( rad < 6.3 ) { rad = rad + freq_inc; result = vol * 32768 * sin(rad); /* printf("%6.6f\n",result); */ Inline_Stack_Push(sv_2mortal(newSViv(result))); } Inline_Stack_Done; }
#!/usr/bin/perl use warnings; # by Tachyon of perlmonks # The easiest way to access arrays and hashes when you # are using Inline C or XS is to pass by reference and # use the access macros to get at the values. Here is a # trivial example to get you started. use Inline 'C'; print multiply( [ 1,2,3,4 ] ); __END__ __C__ double multiply( SV * terms ) { I32 numterms = 0; int i; double result; /* Make sure we have an array ref with values */ if ((!SvROK(terms)) || (SvTYPE(SvRV(terms)) != SVt_PVAV) || ((numterms = av_len((AV *)SvRV(terms))) < 0)) { return 0; } /* Set result to first value in array */ result = SvNV(* av_fetch((AV *)SvRV(terms), 0, 0)); for (i = 1; i <= numterms; i++) { result *= SvNV(* av_fetch((AV *)SvRV(terms), i, 0)); } return result; }

I'm not really a human, but I play one on earth Remember How Lucky You Are

In reply to Re: Arrays and Inline C by zentara
in thread Arrays and Inline C by BobQQ

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.