title: How to iterate through and copy double pointer in C
body: <p>I've created a two dimensional integer array in C, initialised it with values, and then cast it to an <code>int**</code> (I have to do that because it's for homework).<br>
  I've managed to iterate through it and set all of the values to 0. However, when I iterate through it again and print the values it, the output is not <em>all</em> zeros.</p>
  
  <p>Here is a minimum working example:</p>
  
  <pre><code>#include &lt;string.h&gt;
  #include &lt;stdio.h&gt;
  
  #define ROWS    3
  #define COLS    2
  
  int main(void)
  {
      /* Create array and convert to double pointer */
      int c[ROWS][COLS] = {{1,2},{3,5},{8,13}};
      int **ptr = (int **)c;
  
      /* Loop through entire array and print then double each value. */
      int *temp[ROWS];
      for(int i = 0; i&lt;ROWS; i++){
          temp[i] = (int*)(ptr+i);
          for(int j = 0; j&lt;COLS; j++){
              printf("Before setting: %i\n", temp[i][j]);
              temp[i][j] = temp[i][j]*2;
          }
      }
  
      /* Copy temp back into ptr */
      memcpy(ptr, temp, sizeof(ptr));
  
      /* Loop through array and print values */
      int *temp2[ROWS];
      for(int i = 0; i&lt;ROWS; i++){
          temp2[i] = (int*)(ptr+i);
          for(int j = 0; j&lt;COLS; j++){
              printf("After setting: %i\n", temp2[i][j]);
          }
      }
  
  }
  </code></pre>
  
  <p>The issue is that the results are not what I would expect. One time I ran it, this was the output:  </p>
  
  <blockquote>
    <p>Before setting: 1<br>
    Before setting: 2<br>
    Before setting: 3<br>
    Before setting: 5<br>
    Before setting: 8<br>
    Before setting: 13<br>
    After setting: -1193330832<br>
    After setting: 32764<br>
    After setting: 6<br>
    After setting: 10<br>
    After setting: 16<br>
    After setting: 26  </p>
  </blockquote>
  
  <p>The value <code>32764</code> is the same every time the program is run, but the value <code>-1193330832</code> changes each time (I assume it is the memory address of the array).</p>
  
  <p>The output I was expecting is:</p>
  
  <blockquote>
    <p>Before setting: 1<br>
    Before setting: 2<br>
    Before setting: 3<br>
    Before setting: 5<br>
    Before setting: 8<br>
    Before setting: 13<br>
    After setting: 1<br>
    After setting: 4<br>
    After setting: 6<br>
    After setting: 10<br>
    After setting: 16<br>
    After setting: 26<br>
    because the values in the first loop have been doubled.</p>
  </blockquote>
  
  <p>What have I done wrong? Why are the values changing and how should I actually go about fixing this?</p>
  
  <p>(P.S. the homework doesn't involve finding a way to iterate through the double pointer, but I need to be able to do it to complete the actual task)</p>