title: 2-dimensional pointer to output a matrix
body: <p>I want to create a non-rectangular matrix that can output something like this:</p>
  
  <pre><code>0 0 0 0 0 0
  0 0 0
  0 0 0 0 
  0 0 0 0 0 0
  </code></pre>
  
  <p>using 2-dimensional pointers.</p>
  
  <p>I have a 1D pointer that points to an array which stores the size of each row : {6, 3, 4, 6, -1}. </p>
  
  <p>-1 is a flag that will stop the while loop.</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  
  template&lt;class T&gt;
  T** allocate(int* sizes) {
  
      T** twod; // 2-dimensional pointer
  
      while (*sizes != -1) { // the loop will stop when *sizes hit -1
          *twod = new T[*sizes];
          twod++;
      }
  
      return twod;
  }
  
  int main() {
      int array[] = {6,3, 4, 6, -1};
      int* sizes = array; // 1-D pointer to the array which stores the size of each row
  
      int** twod = allocate&lt;int&gt;(sizes);
  
      for(int i = 0; i &lt; 5; i++){
          for(int j = 0; j &lt; *(sizes+i); j++){
              twod[i][j] = 0;
              cout &lt;&lt; twod[i][j] &lt;&lt; " ";
          }
          cout &lt;&lt; endl;
      }
  }
  </code></pre>
  
  <p>The program doesn't output anything. Can you guys help me point out the problem here? I think it might have to do with the while loop. </p>
  
  <p>Thanks a lot!</p>
  
  <p>(This is an assignment of mine regarding the use of pointers. I have to use pointers to output something above. I know using static arrays are much easier)</p>