title: Better idiom for filling a C++ array from a NumericVector
body: <p>I am using <code>Rcpp</code> to wrap an algorithm written (not by me) in C-like C++ (no STL, no boost, no nothing, as far as I can tell).  You can see the implemented algorithm <a href="http://people.sc.fsu.edu/~jburkardt/cpp_src/kmeans/kmeans.cpp" rel="nofollow">here</a> (I'm wrapping <code>kmeans_w_03</code>).  Consequently I'm passing in a <code>numeric</code> vector from R, which then needs to be converted to a <code>double</code> array.</p>
  
  <p>Currently I'm looping element-by-element and filling the one from the 'tother, like this:</p>
  
  <pre><code>SEXP testfn(SEXP weightvec, SEXP cluster_num_k){
      Rcpp::NumericVector weightR(weightvec) ;
      int point_num = weightR.size();
      double weight[point_num] ;
      for(int i = 0; i &lt; point_num; ++i) {
        weight[i] = weightR[i];
      }
  }
  </code></pre>
  
  <p>But with single-element numeric vectors, I can take advantage of Rcpp's beautiful <code>as</code> casting functionality:</p>
  
  <pre><code>int cluster_num = Rcpp::as&lt;int&gt;(cluster_num_k);
  </code></pre>
  
  <p>Trying something similar for length > 1 numeric vectors, however, leads to a crash or an error, depending on the exact variant of the syntax:</p>
  
  <pre><code>double weight[point_num] = Rcpp::as&lt;double&gt;(weightvec);
  </code></pre>
  
  <p>I don't necessarily mind the loop, but I'm a total neophyte and suspect there's a better way.  I've read through Rcpp-introduction, hadley's wiki tutorial, and RcppExamples and not yet found anything that addresses this question, but that doesn't mean I didn't just miss it.  My read of the Doxygen Rcpp docs is that <code>as</code> can cast to a STL vector but not an array (but I have a very hard time reading those docs, so I suspect I'm wrong there).  If so, I guess I could cast to a vector and thence to an array....</p>
  
  <p>So my question: is there a better (fewer lines of code, more expressive code, and perhaps even able-to-avoid-a-memory-allocation) way to convert a <code>NumericVector</code> to a <code>double[]</code>?</p>