-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 8.1 (Completed)
256 lines (216 loc) · 6.46 KB
/
Exercise 8.1 (Completed)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
1. (Review) What are the three items associated with the variable named total?
The variable named total has these 3 items associated with it:the value stored in the variable, the number of bytes reserved for the variable,
and where in memory these bytes are located
2. (Review) If average is a variable, what does &average mean?
If average is a varible than &average is the address of the variable average.
3. (Practice) For the variables and addresses in Figure 8.9, determine the addresses corresponding
to the expressions &temp, &dist, &date, and &miles.
*See textbook for Figure 8.9 Memory bytes*
&temp = 16892
&dist = 16896
&data = 16900
&miles = 16908
4. (Practice) a. Write a C++ program that includes the following declaration statements. Have
the program use the address operator and a cout statement to display the addresses corresponding
to each variable.|
int num, count;
long date;
float slope;
double yield;
#include <iostream>
using namespace std;
int main(){
int num, count;
long date;
float slope;
double yield;
cout << "The address of num is " << &num << endl;
cout << "The address of count is " << &count << endl;
cout << "The address of date is " << &date << endl;
cout << "The address of slope is " << &slope << endl;
cout << "The address of yield is " << &yield << endl;
system("PAUSE");
return 0;
}
b. After running the program written for Exercise 4a, draw a diagram of how your computer
has set aside storage for the variables in the program. On your diagram, fill in the addresses
the program displays.
num ->|00B3FB0C|->storage set aside last hex digits |0D||0E||0F|
count ->|00B3FB00|->storage set aside last hex digits |01||02||03|
data ->|00B3FAF4|->storage set aside last hex digits |F5||F6||F7|
slope ->|00B3FAE8|->storage set aside last hex digits |E9||EA||EB|
yield ->|00B3FAD8|->storage set aside last hex digits |D9||DA||DB|
c. Modify the program written in Exercise 4a (using the sizeof() operator discussed in
Section 2.1) to display the amount of storage your computer reserves for each data type.
With this information and the address information provided in Exercise 4b, determine
whether your computer set aside storage for the variables in the order in which they were
declared.
#include <iostream>
using namespace std;
int main(){
int num, count;
long date;
float slope;
double yield;
cout << "The address of num is " << sizeof(&num) << endl;
cout << "The address of count is " << sizeof(&count) << endl;
cout << "The address of date is " << sizeof(&date) << endl;
cout << "The address of slope is " << sizeof(&slope) << endl;
cout << "The address of yield is " << sizeof(&yield) << endl;
cout << sizeof(num) << endl;
cout << sizeof(count) << endl;
cout << sizeof(date) << endl;
cout << sizeof(slope) << endl;
cout << sizeof(yield) << endl;
system("PAUSE");
return 0;
}
Yes, the computer sets aside storage for the variables in the order in which they were declared
5. (Review) If a variable is declared as a pointer, what must be stored in the variable?
A address is stored in the pointer variable and the data type it points to is declared.
6. (Practice) Using the indirection operator, write expressions for the following:
a. The variable pointed to by xAddr
*xAddr;
b. The variable whose address is in yAddr
*yAddr;
c. The variable pointed to by ptYld
*ptYld;
d. The variable pointed to by ptMiles
*ptMiles;
e. The variable pointed to by mptr
*mptr;
f. The variable whose address is in pdate
*pdate;
g. The variable pointed to by distPtr
*distPtr;
h. The variable pointed to by tabPt
*tabPt;
i. The variable whose address is in hoursPt
*hoursPt;
7. (Practice) Write declaration statements for the following:
a. The variable pointed to by yAddr is an integer.
int *yAddr;
b. The variable pointed to by chAddr is a character.
char *chAddr;
c. The variable pointed to by ptYr is a long integer.
long *ptYr;
d. The variable pointed to by amt is a double-precision variable.
double *amt;
e. The variable pointed to by z is an integer.
int *z;
f. The variable pointed to by qp is a single-precision variable.
float *qp;
g. datePt is a pointer to an integer.
int *datePt;
h. yldAddr is a pointer to a double-precision variable.
double *yldAddr;
i. amtPt is a pointer to a single-precision variable.
float *amtPt;
j. ptChr is a pointer to a character variable.
char *ptChr;
8. (Review) a. What are the variables yAddr, chAddr, ptYr, amt, z, qp, datePt, yldAddr,
amtPt, and ptChr used in Exercise 7 called?
Pointers functions
b. Why are the variable names amt, z, and qp used in Exercise 7 not good choices for
pointer names?
They have no indicators to what they are being used for and can be hard to keep track of in a program.
9. (Practice) Write English sentences that describe what’s contained in the following declared
variables:
a. char *keyAddr;
keyAddr is a pointer to a character variable.
b. int *m;
m is a pointer to an integer
c. double *yldAddr;
yldAddr is a pointer to a double-precision varable.
d. long *yPtr;
yPtr is a pointer to an integer
e. double *pCou;
pCou is a pointer to a double-precision varable.
f. int *ptDate;
ptDate is a pointer to an integer.
10. (Practice) Which of the following is a declaration for a pointer?
a. long a;
Not a declaration
b. char b;
Not a declaration
c. char *c;
declaration
d. int x;
Not a declaration
e. int *p;
declaration
f. double w;
Not a declaration
g. float *k;
declaration
h. float l;
Not a declaration
i. double *z;
declaration
11. (Practice) For the following declarations,
int *xPt, *yAddr;
long *dtAddr, *ptAddr;
double *ptZ;
int a;
long b;
double c;
determine which of the following statements is valid:
a. yAddr = &a;
Valid
b. yAddr = &b;
Not Valid
c. yAddr = &c;
Not Valid
d. yAddr = a;
Not Valid
e. yAddr = b;
Not Valid
f. yAddr = c;
Not Valid
g. dtAddr = &a;
Not Valid
h. dtAddr = &b;
Valid
i. dtAddr = &c;
Not Valid
j. dtAddr = a;
Not Valid
k. dtAddr = b;
Not Valid
l. dtAddr = c;
Not Valid
m. ptZ = &a;
Not Valid
n. ptAddr = &b;
Valid
o. ptAddr = &c;
Not Valid
p. ptAddr = a;
Not Valid
q. ptAddr = b;
Not Valid
r. ptAddr = c;
Not Valid
s. yAddr = xPt;
Valid
t. yAddr = dtAddr;
Not Valid
u. yAddr = ptAddr;
Not Valid
12. (Practice) For the variables and addresses in Figure 8.10, fill in the data determined by the
following statements:
a. ptNum = &m;
8096
b. amtAddr = &amt;
16256
c. *zAddr = 25;
8024
d. k = *numAddr;
10132
e. ptDay = zAddr;
20492
f. *ptYr = 2011;
15010
g. *amtAddr = *numAddr;
10132
*See Textbook for Figure 8.10 Memory locations for Exercise 12*