Source of pointer_parameters.cpp


  1: //pointer_parameters.cpp
  2: //This program illustrates the critical difference between
  3: //passing a "regular" static variable to a function and
  4: //passing a pointer variable. The "big takeaway" from this
  5: //example is realizing that although pass by value (the
  6: //default passing mechanism in C++) applies in both cases,
  7: //the overall effect is quite different, because (and you
  8: //should remember this phrase) "a copy of an address is
  9: //still an address".

 11: #include <iostream>
 12: using namespace std;

 14: void DoIt
 15: (
 16:     int i,  //in
 17:     int* ip //in
 18: )
 19: {
 20:     i *= i;
 21:     *ip *= *ip;
 22: }

 24: int main()
 25: {
 26:     int i = 6;
 27:     int* ip;
 28:     ip = new int;
 29:     *ip = 8;

 31:     cout << "\nBefore the function call:\n";
 32:     cout << "i = " << i << "\t*ip = " << *ip << endl;
 33:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 34:     DoIt(i, ip);
 35:     cout << "\nAfter the function call:\n";
 36:     cout << "i = " << i << "\t*ip = " << *ip << endl;
 37:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 38: }