title: Regarding pointers and arrays and how they are assigned in memory in C++
body: <p>So I am trying to solve this question:</p>
  <blockquote>
  <p>Data is fed in the following input format. The first line contains two space-separated integers denoting the number of variable-length arrays, <code>n</code>, and  the number of queries, <code>q</code>. Each line  of the  subsequent lines contains a space-separated sequence in the format</p>
  <p>k A<sub>i</sub>[0] A<sub>i</sub>[1] … A<sub>i</sub>[k-1]</p>
  <p>where <strong>k</strong> is the length of the array, <strong>A<sub>i</sub></strong>, and is followed by the <strong>k</strong> elements of <strong>A<sub>i</sub></strong>. Each of the  subsequent lines contains two space-separated integers describing the respective values of array number (ranging from <strong>0 to n-1</strong>) and index in that particular array (ranging from <strong>0 to k<sub>i</sub></strong>) for a query. i.e, Given the following input:</p>
  </blockquote>
  <pre><code>3 3
  3 1 2 3
  5 4 5 6 7 8
  4 9 10 11 12 
  0 1
  1 3
  2 0
  </code></pre>
  <blockquote>
  <p>This output is expected</p>
  </blockquote>
  <pre><code>2
  7
  9
  </code></pre>
  <p>I am basically a beginner in C++. This is the code I have tried but I feel the address at which each subsequent array is stored is giving me some problems</p>
  <pre><code>int main(){
      int n, q;
      scanf(&quot;%d %d&quot;, &amp;n, &amp;q);
      printf(&quot;n,q = %d, %d\n&quot;, n, q);
      int* row[n];
      for (int i = 0; i &lt; n; i++){
          int k;
          scanf(&quot;%d&quot;, &amp;k);
          printf(&quot;k = %d\n&quot;, k);
          int col[k];
          row[i] = col;
          for (int j = 0; j &lt; k; j++){
              int elem;
              scanf(&quot;%d&quot;, &amp;elem);
              printf(&quot;i,j,elem = %d, %d, %d\n&quot;, i, j, elem);
              col[j] = elem;
              cout &lt;&lt; &quot;address is &quot; &lt;&lt; &amp;(col[j]) &lt;&lt; &quot;\n&quot;;
          }
      }
      for (int query = 1; query &lt;= q; query++){
          int i, j;
          scanf(&quot;%d %d&quot;, &amp;i, &amp;j);
          int answer;
          answer = *(row[i] + j);
          printf(&quot;row[%d][%d] is %d\n&quot;, i, j, answer);
          cout &lt;&lt; &quot;address is &quot; &lt;&lt; &amp;answer &lt;&lt; &quot;\n&quot;;
      }
      return 0;
  }
  </code></pre>
  <p>And this is the output produced:</p>
  <pre><code>n,q = 3, 3
  k = 3
  i,j,elem = 0, 0, 1
  address is 0x7ffe236edb70
  i,j,elem = 0, 1, 2
  address is 0x7ffe236edb74
  i,j,elem = 0, 2, 3
  address is 0x7ffe236edb78
  k = 5
  i,j,elem = 1, 0, 4
  address is 0x7ffe236edb60
  i,j,elem = 1, 1, 5
  address is 0x7ffe236edb64
  i,j,elem = 1, 2, 6
  address is 0x7ffe236edb68
  i,j,elem = 1, 3, 7
  address is 0x7ffe236edb6c
  i,j,elem = 1, 4, 8
  address is 0x7ffe236edb70
  k = 4
  i,j,elem = 2, 0, 9
  address is 0x7ffe236edb60
  i,j,elem = 2, 1, 10
  address is 0x7ffe236edb64
  i,j,elem = 2, 2, 11
  address is 0x7ffe236edb68
  i,j,elem = 2, 3, 12
  address is 0x7ffe236edb6c
  row[0][1] is 32766
  address is 0x7ffe236edbcc
  row[1][3] is 32766
  address is 0x7ffe236edbcc
  row[2][0] is 3
  address is 0x7ffe236edbcc
  </code></pre>
  <p>Basically, I find that the array addresses are overlapping. Also, The answer computation by dereferencing is resulting in unexpected outputs. Any explanation to the mistakes made here would be appreciated.</p>