Guest User

Sophisticated example with pointers and array

a guest
Sep 17th, 2012
48
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <cstdio>
  2.  
  3. int main() {
  4.   int a[10] = {1, 2, 3, 4, 5};
  5.   int *p1 = a + 4;
  6.  
  7.   --p1;
  8.   int *p2 = p1 - 2;
  9.  
  10.   p2[1]++;
  11.  
  12.   p1[2] = ++a[3];
  13.  
  14.   ++p1;
  15.   p1[-1] = 19;
  16.  
  17.   *p1 = 17;
  18.  
  19.   *(p2-1) = 3;
  20.  
  21.   for (int i = 0; i < 10; ++i)
  22.     printf("%d ", a[i]);
  23.  
  24.   return 0;
  25.   //Output: 3 2 4 19 17 5 0 0 0 0
  26. }
RAW Paste Data