title: Treating a character array as an integer - Learn C the Hard Way Extra credit
body: <p>In Zed Shaw's "Learn C the Hard Way", exercise 9 (<a href="http://c.learncodethehardway.org/book/ex9.html" rel="nofollow">http://c.learncodethehardway.org/book/ex9.html</a>) there is an extra credit question that I find interesting. He defines a 4-character array and asks the reader to figure out how to use the array as a 4-byte integer.</p>
  
  <p>At this point I know just enough to be dangerous, and I was thinking the answer is something along these lines:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int main(int argc, char *argv[])
  {
      char name[4] = {'A'};
  
      int *name_int;
      name_int = &amp;name;
      printf("%d", *name_int);
  
      return 0;
  }
  </code></pre>
  
  <p>My thoughts being that if I created an int pointer with a value being the address of the array that the int type would use the byte of data in that address, followed by the next 3 bytes of data available. In my limited understanding, I am under the impression that both an int and an array would use memory in the same way: starting at an arbitrary memory address than using the next address in sequence, and so on.</p>
  
  <p>However, the output of this isn't what I expected: I get the ascii value of 'A'. Which to me seems to indicate that my solution is incorrect, my understanding how memory is handled is incorrect, or both.</p>
  
  <p>How can this little hack be accomplished and where is it I am going wrong? I am hoping to walk away from this with a better understanding of how pointers and references work, and how memory is stored and used.</p>
  
  <p>Thank you!</p>