title: Copying Byte Pattern For Floats Does Not Work
body: <p>I am currently teaching myself c++ and learning all I can about memory. I found out that you can use a char pointer to copy the bit pattern of an int for example and store it in memory with casting:</p>
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  int main()
  {
  
  int x = 20;
  
  char* cp = new char[sizeof(int)];
  
  cp[0] = *((char*)&amp;x);
  cp[1] = *((char*)&amp;x+1);
  cp[2] = *((char*)&amp;x+2);
  cp[3] = *((char*)&amp;x+3);
  
  std::cout &lt;&lt; (int)*cp; // returns 20; 
  
  
  return 0;
  }
  </code></pre>
  <p>the code above works, when I cast cp to a int so the compiler reads 4 bytes at a time, I get the correct number which is 20.</p>
  <p>However changing it to a float:</p>
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  int main()
  {
  
  float x = 20;
  
  char* cp = new char[sizeof(float)];
  
  cp[0] = *((char*)&amp;x);
  cp[1] = *((char*)&amp;x+1);
  cp[2] = *((char*)&amp;x+2);
  cp[3] = *((char*)&amp;x+3);
  
  std::cout &lt;&lt; (float)*cp; // returns 0.
  
  
  return 0;
  }
  </code></pre>
  <p>returns 0. Now I am a bit confused here. If I am copying every single byte, why is it still giving me a 0? If someone could help me out understanding this it would be very awesome.</p>