-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClass720.cpp
executable file
·163 lines (96 loc) · 2.45 KB
/
Class720.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//============================================================================
// Name : Class720.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iomanip>
#include <iostream>
using namespace std;
void sample1( ) {
int* p1;
int * p2;
int *p3; //p1, p2, and p3 are all pointers to an int
int* a, b; //WATCH OUT: a is a pointer to int but b is a regular int variable
//The values of pointers are addresses so you cannot assign a data value to them directly.
//We need the dereferencing operator. But before we apply this operator, we need to
//create a memory location to store a data value.
p1 = new int; //DYNAMIC ALLOCATION, Yeah!! Now we have a box to store an int
//The 'new' operation returns int* an address of a newly created int box
*p1 = 45;
p2 = p1; //Now we have two pointers referring to the same data
p3 = new int; //another box
*p3 = *p1; //data in a box pointed by p1 is copied to another box pointed by p3
cout << *p1 << " " << *p2 << " " << *p3 << endl;
*p3 = 20;
cout << *p1 << " " << *p2 << " " << *p3 << endl;
*p1 = 50;
cout << *p1 << " " << *p2 << " " << *p3 << endl;
}
void sample2( ) {
int n;
int* p;
n = 100;
p = &n; //& is the address-of operator
//at this point, p points to the int box named 'n'
cout << n << " " << *p << endl;
}
void exchange1(int a, int b) {
int t = a;
a = b;
b = t;
}
void exchange2(int* a, int* b) {
int t;
t = *a;
*a = *b;
*b = t;
}
void sample3( ) {
int i = 10;
int j = 20;
exchange1(i, j);
cout << i << " " << j << endl;
int* p1;
int* p2;
p1 = &i; p2 = &j;
exchange2(p1, p2);
cout << *p1 << " " << *p2 << endl;
}
void clear(int list[ ], int size) { //We are actually passing a pointer to the first position of an array
for (int i = 0; i < size; i++) {
list[i] = 0;
}
}
void clear2(int *list, int size) {
for (int i = 0; i < size; i++) {
list[i] = 0;
}
}
void clear3(int list[], int size) {
int* p;
p = list;
int cnt = 0;
while (cnt < size) {
*p = 0;
p = p + 1;
cnt++;
}
}
void sample4( ) {
int num[ ] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
clear(num, 10);
clear2(num, 10);
//clear3(num, 10);
for (int i = 0; i < 10; i++) {
cout << " " << num[i];
}
}
int main() {
sample1();
sample2();
sample3();
sample4( );
return 0;
}