title: Converting 2D vector to 2D array
body: <p>It's been a while since I last visited arrays (I've been working with vectors recently) and I need to convert an 2D vector back into a 2D array because of a library I am using accepts the paramaters of type <code>double array</code> where the accessors of this array is <code>foo[i][j]</code> for example.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>double** setupHMM(vector&lt;vector&lt;double&gt; &gt; &amp;vals, int N, int M)
  {
    double** temp;
    temp = new double[N][M];
  
   for(unsigned i=0; (i &lt; N); i++)
   {
      for(unsigned j=0; (j &lt; M); j++)
      {
          temp[i][j] = vals[i][j];
      }
   }
  }
  </code></pre>
  
  <p>And with this, I get <code>error: ‘M’ cannot appear in a constant-expression</code></p>
  
  <p>I have also tried the following:</p>
  
  <pre><code>double** setupHMM(vector&lt;vector&lt;double&gt; &gt; &amp;vals, int N, int M)
  {
     double** temp;
  
     for(unsigned i=0; (i &lt; N); i++)
     { 
        temp[i] = new double[N];
        for(unsigned j=0; (j &lt; M); j++)
        {
            temp[j] = new double[M];
            temp[i][j] = vals[i][j];
        } 
     }
   }
  </code></pre>
  
  <p>However, this produces a segmentation fault 11. </p>
  
  <p>Could anyone suggest any advice, or, a better way to convert a vector to a 2D array..</p>
  
  <p>Thanks </p>