title: Incrementing pointer to array
body: <p>I came across this program on HSW:</p>
  
  <pre><code>int *p;
  int i;
  
  p = (int *)malloc(sizeof(int[10]));
  for (i=0; i&lt;10; i++)
      *(p+i) = 0;
  free(p);
  </code></pre>
  
  <p>I don't understand the loop fully.
  Assuming the memory is byte addressable, and each integer takes up 4 bytes of memory, and say we allocate 40 bytes of memory to the pointer <code>p</code> from address <code>0</code> to <code>39</code>.<br>
  Now, from what I understand, the pointer <code>p</code> initially contains value <code>0</code>, i.e. the address of first memory location. In the loop, a displacement is added to the pointer to access the subsequent integers.  </p>
  
  <p>I cannot understand how the memory addresses uptil <code>39</code> are accessed with a displacement value of only <code>0 to 9</code>. I checked and found that the pointer is incremented in multiples of 4. How does this happen? I'm guessing it's because of the integer type pointer, and each pointer is supposedly incremented by the size of it's datatype. Is this true?</p>
  
  <p>But what if I actually want to point to memory location 2 using an integer pointer. So, I do this: <code>p = 2</code>. Then, when I try to de-reference this pointer, should I expect a segmentation fault?</p>