// example4.cc
// CMSC15100-2
// Winter 2005
// intro to arrays

#include <iostream>

using namespace std;

int main()
{
  int b;
  int a[4];   // array of length 4
  int c[] = {1,2,4,9};
  int *p;

  a[0] = 79; // array item as an lvalue
  a[1] = 2*a[0];
  b = ( a[0] + a[1] ) / 2;  // array items as rvalues

  c[(2+3)/3] = 7;

  a[4] = 42;  // What happens here?
  // probably (but not always) it overwrites c[0].
  cout << c[0] << endl;

  cout << ( &( a[4] ) == &( c[0] ) ) << endl;

  // point a pointer to the head of the array
  p = a;
  // print the value -- this is a[0]
  cout << *p << endl;
  cout << a[2] << endl;
  *(p+2) = 42;     // edit a[2] with pointer notation
  cout << a[2] << endl;

  // dynamically allocated array from the heap
  // of length 3
  p = new int[3];
 
  // use the array 
  p[0] = 1;
  p[1] = 2;
  p[2] = 4;

  // return the array to the heap...
  delete []p;

  return 0;
}
