// ex2: show the use of pointer variables
// indirection 
// pointer assignment
// "dangling pointer" pointer that has garbage in it


#include <iostream>
using namespace std;

int main()
{

  int number=99;
  float amount;
  int x=1, y=2;

  int *iptr; // iptr is a variable and its of type pointer to int
  iptr = &number;  // iptr should point to the variable number

// you must specify what type of data a ptr is pointing to
  float *fptr; // defines a variable that can store a pointer to float
//  fptr = &number; compiler error

/************
NEVER USE A POINTER VARIABLE BEFORE ASSIGNING IT 
************/
//  fptr=(float*)5;
  cout << "fptr is: " << fptr << endl;  // garbage 
  cout << "contents of fptr is: " << *fptr << endl; // garbage pointer to who knows where 

  
  cout << "address of number is: " << &number << endl;
  cout << "value of number is:  " << number << endl;

  cout << "iptr is: " << iptr << endl;
// indirection operator gives contents of a pointer
  cout << "contents of iptr is: " << *iptr << endl;  

  *iptr = 109;
  cout << "NOW, after assigning 109 to *iptr..." << endl;
  cout << "iptr is: " << iptr << endl;
// indirection operator gives contents of a pointer
  cout << "contents of iptr is: " << *iptr << endl;  
  cout << "value of number is:  " << number << endl;

// now change iptr to point to x
  iptr = &x;
  *iptr=7000;
  cout << "NOW, after changing iptr to point to x..." << endl;
  cout << "iptr is: " << iptr << endl;
  cout << "contents of iptr is: " << *iptr << endl;  
  cout << "value in x is: " << x << endl;  
  cout << "value of number is:  " << number << endl;

// pointer assignment

  int *ptr2; // another pointer variable
  ptr2=&x;  // ptr2 point to x 
  ptr2=iptr; // assign the address in iptr to ptr2 (that is address of x)!
 // iptr=fptr; compiler type error
 cout << "ptr2 is " << ptr2 << " &x " << &x << " iptr " << iptr << endl;
  return 0;
}
