Advertisement
Guest User

Pointers & const

a guest
Jun 24th, 2013
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.         int i1 = 5, i2 = 3;
  8.         const int* pi1 = &i1;
  9.         int* const pi2 = &i2;
  10.  
  11.         // the next line prints 5 3
  12.         cout << *pi1 << ' ' << *pi2 << endl;
  13.  
  14.         // the next line is compile error - try to modify to const int
  15.         // *pi1 = 7;
  16.  
  17.         pi1 = &i2;
  18.         // the next line prints 3 3
  19.         cout << *pi1 << ' ' << *pi2 << endl;
  20.  
  21.         // the next line is compile error - try to modify to const int pointer
  22.         // pi2 = &i2;
  23.  
  24.         pi1 = &i1;
  25.         *pi2 = 7;
  26.         // the next line prints 5 7
  27.         cout << *pi1 << ' ' << *pi2 << endl;
  28.  
  29.         return 0;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement