forked from nattee/data2015
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpointer.cpp
41 lines (31 loc) · 1020 Bytes
/
pointer.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
#include <iostream>
using namespace std;
void test(int *T) {
int xyz;
T = &xyz;
}
int main() {
int x,y,z;
x = 10; y = 1; z = 2;
//int *x means that x is the pointer of int
int *px,*py,*pz;
//&x in expression is the `address` of the variable
px = &x; py = &y; pz = &z;
//we can get the value where px points to by *px
cout << "x = " << x << endl;
cout << "*px = " << *px << endl;
cout << "&x = " << &x << endl;
cout << "px = " << px << endl;
cout << endl << "before" << endl;
cout << "x: address = " << px << " value = " << x << endl;
cout << "y: address = " << py << " value = " << y << endl;
cout << "z: address = " << pz << " value = " << z << endl;
test(px);
cout << "x: address = " << px << " value = " << x << endl;
pz = pz + 2;
*pz = 100;
cout << endl << "afer" << endl;
cout << "x: address = " << px << " value = " << x << endl;
cout << "y: address = " << py << " value = " << y << endl;
cout << "z: address = " << pz << " value = " << z << endl;
}