Point taken: STL looks like the way to go in general.  <div><br></div><div>In my particular example, however, the arrays get immediately cast to another structure foo (somebody else&#39;s code).  So I need to cast from IntegerVector to int[] before they get cast to foo[].  What you suggested works perfectly (and makes sense in hindsight).</div>

<div><br></div><div>Is the story identical for char[]?  Where x is a CharacterVector, I tried </div><div><br></div><div>char* a = x.begin();</div><div><br></div><div>and got</div><div>error: cannot convert ‘Rcpp::Vector&lt;16&gt;::iterator’ to ‘char*’ in assignment</div>

<div><br></div><div>Any help much appreciated.</div><div>Chris<br><br><div class="gmail_quote">On Fri, Aug 12, 2011 at 3:19 PM, Dirk Eddelbuettel <span dir="ltr">&lt;<a href="mailto:edd@debian.org">edd@debian.org</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div><div></div><div class="h5"><br>
On 12 August 2011 at 14:50, Chris DuBois wrote:<br>
| Hi all,<br>
|<br>
| I&#39;m trying to figure out how to pass in an array of integers to a function<br>
| inside a module.  For example, adding the following function to runit.Module.R<br>
| works fine:<br>
|<br>
|         int bla3( IntegerVector x ) {<br>
|               return sum(x);<br>
|         }<br>
|<br>
| However, I need to pass an int array, rather than an IntegerVector.  Using int<br>
| x[] in the arguments doesn&#39;t compile (though I&#39;m unfamiliar with C++ in<br>
| general, so maybe this shouldn&#39;t work anyway).  <br>
|<br>
| Alternatively, should I just cast x from an IntegerVector to an int array?  I<br>
| tried various permutations of as, vector, &lt;int&gt;, etc, and would like to learn<br>
| the proper way of doing this.<br>
<br>
</div></div>You generally do not want old school x[] arrays in C++. Why?  Because STL<br>
vectors do _everything_ they do at (essentially) zero added cost, free you<br>
from malloc/free and still allow you to access the straight memory should you<br>
need to (to talk to a C API, say).<br>
<br>
So use IntegerVector for _the interface_. You can the, if you must, do<br>
<br>
     IntegerVector x;<br>
<br>
     int a1[] = x.begin();     // STL-style iterator to beginning of memory<br>
     int *a2  = x.begin();     // idem<br>
<br>
Hope this helps,  Dirk<br>
<font color="#888888"><br>
--<br>
Two new Rcpp master classes for R and C++ integration scheduled for<br>
New York (Sep 24) and San Francisco (Oct 8), more details are at<br>
<a href="http://dirk.eddelbuettel.com/blog/2011/08/04#rcpp_classes_2011-09_and_2011-10" target="_blank">http://dirk.eddelbuettel.com/blog/2011/08/04#rcpp_classes_2011-09_and_2011-10</a><br>
<a href="http://www.revolutionanalytics.com/products/training/public/rcpp-master-class.php" target="_blank">http://www.revolutionanalytics.com/products/training/public/rcpp-master-class.php</a><br>
</font></blockquote></div><br></div>