title: Copy contents of one array to another with pointers
body: <p>I've been learning C++ on my own for a while now, and I've come to a "roadblock" when it comes to pointers. I'm using this as my <a href="http://www.cplusplus.com/doc/tutorial/pointers/" rel="nofollow">http://www.cplusplus.com/doc/tutorial/pointers/</a> learning material, but I'm still having an issue. So to test something I wanted to copy the contents of one array into another. I wrote the following.</p>
  
  <pre><code>char arrayA[15] = "abcdef";
  char arrayB[15];
  
  char *a = arrayA;
  char *b = arrayB;
  
  cout &lt;&lt; "before loop:" &lt;&lt; endl;
  cout &lt;&lt; a &lt;&lt; endl;
  cout &lt;&lt; b &lt;&lt; endl;
  
  while (*a != '\0') {
      // Copy the contents of a into b
      *b = *a;
  
      // Step
      a++;
      b++;
  }
  
  // Assign null to the end of arrayB
  *b = '\0';
  
  cout &lt;&lt; "after loop:" &lt;&lt; endl;
  cout &lt;&lt; a &lt;&lt; endl;
  cout &lt;&lt; b &lt;&lt; endl;
  </code></pre>
  
  <p>I get the following results.</p>
  
  <pre><code>before loop:
  abcdef
  
  after loop:
  </code></pre>
  
  <p>When I <code>cout</code> the contents before the loop I get the expected results. <code>a</code> contains "abcdef" and <code>b</code> is nothing, because there is no value yet. Now after the loop, both <code>a</code> and <code>b</code> show no results. This is where I am lost. I used <code>*</code> to dereference both <code>a</code> and <code>b</code> and assign the value of <code>a</code> into <code>b</code>. Where did I go wrong? Do I need to use the <code>&amp;</code> with this?</p>
  
  <p><strong>Solution:</strong></p>
  
  <p>After the loop is complete, pointer <code>*a</code> is pointing to the end of arrayA and pointer <code>*b</code> is pointing to the end of arrayB. So to get the full results of arrayB simply <code>cout &lt;&lt; arrayB</code>. Or create a pointer that never changes and always points to arrayB <code>char *c = arrayB</code> and <code>cout &lt;&lt; c</code> at the end of the loop.</p>