title: Why Does This Kind of Pointer Arithmetic Work In A Function, But Not In Main?
body: <p>Here is my code that works. The function initializes the array, a, to values 0 - 3</p>
  
  <pre><code>int main(void) 
  {
      int a[4];
  
      pointer(a);
  
      return 0;
  }
  
  void pointer(int* a)
  {
     int *p, i;
     p = a;
  
     for(i = 0; i &lt; 4; i++)
     {
         *a++ = i;
         printf(" %d", p[i]);
      }
  }
  </code></pre>
  
  <p>But when I combine it all into main(), it no longer works.</p>
  
  <pre><code>int main(void) 
  {
      int a[4], *p, i;
      p = a;
  
      for(i = 0; i &lt; 4; i++)
      {
          *a++ = i;
          printf("%d", p[i]);
      }
  
      return 0;
  }
  </code></pre>
  
  <p>Instead, it prints out memory addresses or something. It works when I dynamically allocate a[], so I'm guessing it has something to do with the way a[] is managed in memory. Can someone tell me why the second main() doesn't work?</p>