forked from uva-cs/pdr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pointers.cpp
61 lines (50 loc) · 1.97 KB
/
pointers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* Original author: Michele Co, CS 216, Spring 2007
* Updated by Aaron Bloomfield, CS 2150, Spring 2017
* Filename: pointers.cpp
* Description: Examples demonstrating pointers and C++ syntax related to pointers
*
* To force 64-bit operation, this file will have to be compiled with
* the -m64 flag
*/
#include <iostream>
using namespace std;
int main() {
// declare and initialize some int variables (we use longs to
// ensure 64-bit operation)
long y = 5;
long x = 1;
// declare a pointer to int and initialize to the address of x
long * x_pointer = &x;
// print the values of each of these variables
cout << "x = " << x << endl;
cout << "y = " << y << endl;
cout << "x_pointer = " << x_pointer << endl;
cout << "*x_pointer = " << *x_pointer << endl << endl;
// change the value of the memory pointed to by x_pointer
// by dereferencing and assigning a new value
*x_pointer = 2;
// print the values of all variables
cout << "After *x_pointer = 2 " << endl;
cout << "x = " << x << endl;
cout << "y = " << y << endl;
cout << "x_pointer = " << x_pointer << endl;
cout << "*x_pointer = " << *x_pointer << endl << endl;
// change the address that x_pointer points to, to y
x_pointer = &y;
// print the values of all variables
cout << "After x_pointer = &y " << endl;
cout << "x = " << x << endl;
cout << "y = " << y << endl;
cout << "x_pointer = " << x_pointer << endl;
cout << "*x_pointer = " << *x_pointer << endl << endl;
// change the value of the memory pointed to by x_pointer
// by dereferencing and assigning a new value
*x_pointer = 3;
// print the values of all variables
cout << "After *x_pointer = 3 " << endl;
cout << "x = " << x << endl;
cout << "y = " << y << endl;
cout << "x_pointer = " << x_pointer << endl;
cout << "*x_pointer = " << *x_pointer << endl << endl;
return 0;
}