Given the
following section of code:
struct studinfo
{ char studentname[30]; ** This means
that each record requires:
short age, credithours; 30 + 2 + 2 + 4 + 4 = 42
bytes
long weightindrams;
float gpa; };
struct studinfo student[6] =
The command: printf(“%lu\n”,&student[3].weightindrams);
yields the output: 7686
Given an array of 6
records, each with 5 fields, our table would appear as:
. |
Studentname |
Age |
credhrs |
wgtindrams |
gpa |
Rec #\Bytes |
30 |
2 |
2 |
4 |
4 |
0 |
|
|
|
|
|
1 |
|
|
|
|
|
2 |
|
|
|
|
|
3 |
|
|
|
7686 |
|
4 |
|
|
|
|
|
5 |
|
|
|
|
|
Where we
are know that &student[3].weightindrams is 7686. Given that we know that address and
we know how many bytes each field uses (and that each record requires 42 bytes
of storage), we can determine the address of every field in the array of
records:
. |
Studentname |
Age |
credhrs |
wgtindrams |
gpa |
Rec #\Bytes |
30 |
2 |
2 |
4 |
4 |
0 |
7526 |
7556 |
7558 |
7560 |
7564 |
1 |
7568 |
7598 |
7600 |
7602 |
7606 |
2 |
7610 |
7640 |
7642 |
7644 |
7648 |
3 |
7652 |
7682 |
7684 |
7686 |
7690 |
4 |
7694 |
7724 |
7726 |
7728 |
7732 |
5 |
7736 |
7766 |
7768 |
7770 |
7774 |
If we fill in
the table with the given data, the table would appear as:
. |
Rec Addr: |
Studentname |
Age |
credhrs |
wgtindrams |
gpa |
Rec #\Bytes |
|
30 |
2 |
2 |
4 |
4 |
0 |
7526 |
Christie,
Agatha |
96 |
82 |
37324 |
3.05 |
1 |
7568 |
Shakespeare,
Bill |
256 |
12 |
52253 |
3.56 |
2 |
7610 |
Albee, Edward |
76 |
120 |
72689 |
2.78 |
3 |
7652 |
Voltaire |
56 |
82 |
64766 |
3.75 |
4 |
7694 |
Chaucer, Geoffry |
420 |
91 |
41566 |
3.25 |
5 |
7736 |
Grisham,
John |
46 |
24 |
66456 |
1.57 |
Now, to
answer our questions:
a. What is the value of student[2].name? From
the above table: Albee, Edward
b. What is the value of student[1].gpa?
From the
above table: 3.56
c. What is the value of student[6].age? Unknown OR Not a valid question
d. What is the value of &student[4]? From the above
table: 7694
e. What the value of
&student[0].credithours?
From our address
table: 7558 OR
From the above table:
7526 + 30 + 2 = 7558
f. What the value of
&student[85].age Given the
base address: 7526
&student[85].age = 7526 + 85 * 42 + 30
= 7526 + 3570 + 30 = 11126