Advertisement
GregLeck

Untitled

Feb 15th, 2022
637
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.72 KB | None | 0 0
  1. #include<iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     // Pointers vs. References
  8.     int myInt = 5;
  9.  
  10.     // Reference to 'myInt'
  11.     int& anotherInt = myInt;
  12.     anotherInt = 10;
  13.    
  14.     // Modified value of 'myInt'
  15.     cout << "myInt = " << myInt << endl;
  16.  
  17.     // Pointer to myInt;
  18.     int* ptr = &myInt;
  19.  
  20.     // Prints address of pointer
  21.     cout << ptr << endl;
  22.     // Dereferencing pointer to show actual content of address
  23.     cout << *ptr << endl;
  24.  
  25.     // Pointers and arrays
  26.     int numbers[] = { 0, 1, 2, 4, 5 };
  27.  
  28.     // Points to first value of array
  29.     int* numPtr = numbers;
  30.  
  31.     // Will print '0'
  32.     cout << *numPtr << endl;
  33.  
  34.     // Will print '1' (pointer arithmetic)
  35.     numPtr++;
  36.     cout << *numPtr << endl;
  37.  
  38.     system("pause");
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement