title: Why is memcpy from int to char not working?
body: <p>I have the hex value <code>0x48656c6c6f</code> for which every byte represents the ASCII value of each character in the string <code>"Hello"</code>. I also have the a <code>char</code> array that I want to insert these values into.</p>
  
  <p>When I had a hex value that was smaller (for example, <code>0x48656c6c</code>, which represents <code>"Hell"</code>), printing out the <code>char</code> array gave the correct output. But the following code prints <code>"olle"</code> (in little-endian) but not <code>"olleH"</code>. Why is this?</p>
  
  <pre><code>#include &lt;iostream&gt;                           
  #include &lt;cstring&gt;                             
  
  int main()
  {
      char x[6] = {0};
      int y = 0x48656c6c6f;
  
      std::memcpy(x, &amp;y, sizeof y);
  
      for (char c : x)
          std::cout &lt;&lt; c;
  }
  </code></pre>
  
  <p><a href="http://ideone.com/woCmAy#view_edit_box" rel="nofollow">Demo is here.</a></p>