title: Difference in values between pointer access and array access
body: <p>Can someone please clarify an incorrect interpretation on my part? I know that my understanding is incorrect due my code's resulting output (see the bottom of the question).
  Thanks in advance.</p>
  
  <p>To clarify, what does each segment of the following line mean?:</p>
  
  <pre><code>*(u8 *)((u32)BufferAddress + (u32)i)
  </code></pre>
  
  <p>And how does it differ from the following line:</p>
  
  <pre><code>*(u32 *)((u32)BufferAddress + (u32)i)
  </code></pre>
  
  <p>My interpretation of the above is:</p>
  
  <ol>
  <li>segment1 = ((u32)BufferAddress + (u32)i) => determine an address as an integer.</li>
  <li>segment2 = (u32 *)(segment1) => cast the address to be treated like a pointer, where the pointer is 32 bits in length.</li>
  <li>segment3 = *(segment2) => dereference the pointer in order to obtain the value of residing at the calculated address.</li>
  </ol>
  
  <p>What is incorrect about my interpretation? I think my lack of understanding is in the segment2 area... What is the difference between casting (u32 *) and (u8 *)?</p>
  
  <p>Here is the code that made me realize I have a knowledge-gap:</p>
  
  <p><b>Initialization code:</b></p>
  
  <pre><code>main(...) {
       ...
       u8 *Buffer = malloc(256);
       ...
       Buffer[0] = 1;
       Buffer[1] = 0;
       Buffer[2] = 0;
       Buffer[3] = 4;
       Buffer[4] = 0;
       Buffer[5] = 0;
       qFunction(... , Buffer, 6, ...);
       ...
  }
  
  qFunction(... , const u8 *BufferPointer, u32 BufferLength, ...) {
       u32 BufferAddress;
       ...
       BufferAddress = (u32) BufferPointer;
       ...
  
       /* Method 1: */
       for (i=0; i &lt; BufferLength; i++)
            printf("%d, %p\n", BufferPointer[i], &amp;BufferPointer[i]);
  
       /* Method 2: */
       for (i=0; i &lt; BufferLength; i++)
            printf("%d, 0x%lx\n", *(u8 *)(BufferAddress+i), BufferAddress+i);
  
       /* Method 3: */
       for (i=0; i &lt; BufferLength; i++)
            printf("%d, 0x%lx\n", *(u32 *)(BufferAddress+i), BufferAddress+i);
       ...
   }
  </code></pre>
  
  <p>The outputs of Method 1 and Method 2 are as I expect (both are the same):</p>
  
  <pre><code>1, 0x1000000
  0, 0x1000001
  0, 0x1000002
  4, 0x1000003
  0, 0x1000004
  0, 0x1000005
  </code></pre>
  
  <p>However, the output of Method 3 seems weird to me; only part of the result is the same as Method 1/2:</p>
  
  <pre><code>-1442840511, 0x1000000
  11141120, 0x1000001
  43520, 0x1000002
  4, 0x1000003
  0, 0x1000004
  0, 0x1000005
  </code></pre>
  
  <p>I'd appreciate any tips or references to reading material.
  Thanks.</p>