title: How does memcpy work on pointers to arrays?
body: <p>I initialise 2 arrays like so:</p>
  
  <pre><code>x = (double*) malloc(sizeof(double)*n);
  xPlus1 = (double*) malloc(sizeof(double)*n);
  </code></pre>
  
  <p>Firstly, I'm not certain of exactly what x or xPlus1 now are, a pointer to an array full of doubles, or an array full of pointers to doubles? :/</p>
  
  <p>The answer to that question probably effects this but if I do these various operations, I can't get it to do what I want, which is just to copy the values from xPlus1 into x. xPlus1 and x do hold different values for each index, because printing after a simple for loop achieves the desired effect:</p>
  
  <pre><code>for (int i = 0; i &lt; n; i++) {
      x[i] = xPlus1[i];
  }
  </code></pre>
  
  <p>But using...</p>
  
  <pre><code>memcpy(x, xPlus1, sizeof(x));
  memcpy(x, &amp;xPlus1, sizeof(x)); 
  </code></pre>
  
  <p>If I access x after either of these in the same function, x is unchanged, kept it's old values.</p>
  
  <pre><code>memcpy(&amp;x, &amp;xPlus1, sizeof(x)); 
  </code></pre>
  
  <p>I believe this makes x and xPlus1 point to the same memory location by the outputs I get. This is because if I modify x or xPlus1 after this copy, then both values change if I print both arrays.</p>
  
  <pre><code>memcpy(&amp;x, xPlus1, sizeof(x)); 
  </code></pre>
  
  <p>Dies, exception: Unhandled exception at 0x011D5626 in MPITemplate.exe: 0xC0000005: Access violation writing location 0x00000000.</p>
  
  <p>The ampersand in functions usually indicate a pass by reference, how does this play out when given to memcpy, and what difference does it make if it's going to write to the same blocks in the end?</p>
  
  <p>I'd appreciate if someone could tell me in detail what's going on with these operations, because I can fiddle around making it work but I'd rather understand what's actually happening when these calls are made.</p>
  
  <p>Thanks,</p>
  
  <p>Mike</p>