Thursday, October 27, 2011

Testing Scopes

#include <iostream>
using namespace std;

int main()
{
  int j = 1;
  cout << j << endl;

  // assigning the address of outer j to jptr
  int* jptr = &j;

  // creating j2ptr to hold the address of inner j
  int* j2ptr;
  cout << jptr
       << "____________________"  << endl;
  int i = 5;
  for (int j = 1; j<i ; j++)
  {
    // this for loop only operates on the
    // inner j
    j2ptr = &j;
    cout << "Rock and Roll!" << j << endl;
  }

  // the outer j is not affected by the
  // above for loop
  cout << j2ptr
       << "____________________"  << endl;
  cout << j << endl;
  return 0;
}

No comments:

Post a Comment