home *** CD-ROM | disk | FTP | other *** search
/ Beginning C++ Through Gam…rogramming (2nd Edition) / BCGP2E.ISO / source / chapter07 / pointing.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-04-11  |  1.6 KB  |  52 lines

  1. // Pointing
  2. // Demonstrates using pointers
  3.  
  4. #include <iostream>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11.     int* pAPointer;    //declare a pointer
  12.     
  13.     int* pScore = 0;   //declare and initialize a pointer
  14.         
  15.     int score = 1000;
  16.     pScore = &score;   //assign pointer pScore address of a variable score
  17.  
  18.     cout << "Assigning &score to pScore\n";
  19.     cout << "&score is: " << &score << "\n";     //address of score variable
  20.     cout << "pScore is: " << pScore << "\n";     //address stored in pointer
  21.     cout << "score is: " << score << "\n";
  22.     cout << "*pScore is: " << *pScore << "\n\n"; //value pointed to by pointer 
  23.    
  24.     cout << "Adding 500 to score\n";
  25.     score += 500;
  26.     cout << "score is: " << score << "\n";
  27.     cout << "*pScore is: " << *pScore << "\n\n";
  28.  
  29.     cout << "Adding 500 to *pScore\n";
  30.     *pScore += 500;
  31.     cout << "score is: " << score << "\n";
  32.     cout << "*pScore is: " << *pScore << "\n\n";
  33.  
  34.     cout << "Assigning &newScore to pScore\n";    
  35.     int newScore = 5000;
  36.     pScore = &newScore;
  37.     cout << "&newScore is: " << &newScore << "\n";
  38.     cout << "pScore is: " << pScore << "\n";     
  39.     cout << "newScore is: " << newScore << "\n";
  40.     cout << "*pScore is: " << *pScore << "\n\n";  
  41.     
  42.     cout << "Assigning &str to pStr\n";
  43.     string str = "score";
  44.     string* pStr = &str;   //pointer to string object
  45.     cout << "str is: " << str << "\n";
  46.     cout << "*pStr is: " << *pStr << "\n";
  47.     cout << "(*pStr).size() is: " << (*pStr).size() << "\n";
  48.     cout << "pStr->size() is: " << pStr->size() << "\n";   
  49.     
  50.     return 0;
  51. }
  52.