2. (20 pt) Trace the following program, show how values change (16pt) ?nmemory,
ID: 3891799 • Letter: 2
Question
2. (20 pt) Trace the following program, show how values change (16pt) ?nmemory, and give the output (4pt). include typedef struct ( name ress ontent V alue int x; int y: ) fractionT; 210] 12 3 z[1] 1614 z121 205 213] 24 7 4] 28 2 215] 32 9 main() int z[6](3, 4, 5, 7, 2, 9) int *pl,*p2; fractionT f[2]: pl 36 p2 40 T0]x 44 T01y 48 *pl++12; *++p 1 = 15; printf("%d %d ", flly 56 %d 2[2], 2[3), z14], p2) %d z [3]func (&f; I0], &E; to1.y, p2) printf("%d %d %d %d ", 100 b 108 ? 112 x 116 y 120 124 int func (fractionT *a, int *b, int **c) int x-8, y-13; OUTPUT 4pt) a->y 25; **c = 17; return a-py yExplanation / Answer
Step 1:
int z[6] = {3,4,5,7,2,9};
Name
z[0]
z[1]
z[2]
z[3]
z[4]
z[5]
P1
P2
f[0].x
f[0].y
f[1].x
f[1].y
a
b
c
x
y
Address
12
16
20
24
28
32
36
40
44
48
52
56
100
104
108
112
116
120
124
Memory Content/ Value
3
4
5
7
2
9
Step 2:
p1 = &z[2];
Address of z[2] which is 20 will be stored in pointer p1
Name
Z[0]
Z[1]
Z[2]
Z[3]
Z[4]
Z[5]
P1
P2
f[0].x
f[0].y
f[1].x
f[1].y
a
b
c
x
y
Address
12
16
20
24
28
32
36
40
44
48
52
56
100
104
108
112
116
120
124
Memory Content/ Value
3
4
5
7
2
9
20
Step 3:
p2 = &p1;
Address of p1 which is 36 will be stored in pointer variable p2
Name
Z[0]
Z[1]
Z[2]
Z[3]
Z[4]
Z[5]
p1
p2
f[0].x
f[0].y
f[1].x
f[1].y
a
b
c
x
y
Address
12
16
20
24
28
32
36
40
44
48
52
56
100
104
108
112
116
120
124
Memory Content/ Value
3
4
5
7
2
9
20
36
Step 4:
*p1++ = 12;
The value 12 will be stored in location pointed by p1 which is address location 20 & 20 is the address location of z[2].
Then pointer p1 will be incremented from 20 to 24(1 integer word= 4 byte) due to post increment.
Name
Z[0]
Z[1]
Z[2]
Z[3]
Z[4]
Z[5]
P1
P2
f[0].x
f[0].y
f[1].x
f[1].y
a
b
c
x
y
Address
12
16
20
24
28
32
36
40
44
48
52
56
100
104
108
112
116
120
124
Memory Content/ Value
3
4
12
7
2
9
24
36
Step 5:
*++p1 = 15;
First pointer p1 will be incremented from 24 to 28(1 integer word= 4 byte) due to pre-increment.
The value 15 will be stored in location pointed by p1 which is address location 28 & 28 is the address location of z[4].
Name
Z[0]
Z[1]
Z[2]
Z[3]
Z[4]
Z[5]
P1
P2
f[0].x
f[0].y
f[1].x
f[1].y
a
b
c
x
y
Address
12
16
20
24
28
32
36
40
44
48
52
56
100
104
108
112
116
120
124
Memory Content/ Value
3
4
12
7
15
9
28
36
Step 6:
printf("%d %d %d %d ", z[2], z[3], z[4], **p2);
12 7 15 15
Step 6:
z[3]=func(&f[0], &f[0].y, p2);
int func(fractionT *a, int *b, int **c)
{
}
The address of f[0] which is 44 is passed to pointer variable *a.
The address of f[0].y which is 48 is passed to pointer variable *b.
The value of pointer variable p2 which is 36 passed to pointer variable *c.
Name
Z[0]
Z[1]
Z[2]
Z[3]
Z[4]
Z[5]
P1
P2
f[0].x
f[0].y
f[1].x
f[1].y
a
b
c
x
y
Address
12
16
20
24
28
32
36
40
44
48
52
56
100
104
108
112
116
120
124
Memory Content/ Value
3
4
12
7
15
9
28
36
44
48
36
Step 7:
int x=8, y=13;
Name
Z[0]
Z[1]
Z[2]
Z[3]
Z[4]
Z[5]
P1
P2
f[0].x
f[0].y
f[1].x
f[1].y
a
b
c
x
y
Address
12
16
20
24
28
32
36
40
44
48
52
56
100
104
108
112
116
120
124
Memory Content/ Value
3
4
12
7
15
9
28
36
44
48
36
8
13
Step 8:
*b = y % x / 2;
*b = 13 % 8 / 2 = 5 /2 = 2
Here pointer b is pointing to address 48 which is f[0].y, so the value of f[0].y=2
Name
Z[0]
Z[1]
Z[2]
Z[3]
Z[4]
Z[5]
P1
P2
f[0].x
f[0].y
f[1].x
f[1].y
a
b
c
x
y
Address
12
16
20
24
28
32
36
40
44
48
52
56
100
104
108
112
116
120
124
Memory Content/ Value
3
4
12
7
15
9
28
36
2
44
48
36
8
13
Step 9:
a++;
Pointer ‘a’ incremented to next address location i.e. location 44 changed to 52 (struct fractionT = 4+4=8 byte)
Name
Z[0]
Z[1]
Z[2]
Z[3]
Z[4]
Z[5]
P1
P2
f[0].x
f[0].y
f[1].x
f[1].y
a
b
c
x
y
Address
12
16
20
24
28
32
36
40
44
48
52
56
100
104
108
112
116
120
124
Memory Content/ Value
3
4
12
7
15
9
28
36
2
52
48
36
8
13
Step 10:
a->y=25;
Name
Z[0]
Z[1]
Z[2]
Z[3]
Z[4]
Z[5]
P1
P2
f[0].x
f[0].y
f[1].x
f[1].y
a
b
c
x
y
Address
12
16
20
24
28
32
36
40
44
48
52
56
100
104
108
112
116
120
124
Memory Content/ Value
3
4
12
7
15
9
28
36
2
25
48
48
36
8
13
Step 11:
**c=17;
‘c’ is pointer to pointer.
**c means 17 is stored in location pointed by 36 which is address 28 and 28 is address of z[4].
So the value of z[4]=17
Name
Z[0]
Z[1]
Z[2]
Z[3]
Z[4]
Z[5]
P1
P2
f[0].x
f[0].y
f[1].x
f[1].y
a
b
c
x
y
Address
12
16
20
24
28
32
36
40
44
48
52
56
100
104
108
112
116
120
124
Memory Content/ Value
3
4
12
7
17
9
28
36
25
48
48
36
8
13
Step 12:
int func(fractionT *a, int *b, int **c)
{
return a->y+y; Here value of a->y = 25 & y=13 So returned value will be 25+13= 38
}
z[3]=func(&f[0], &f[0].y, p2); returned value 38 will be stored in z[3]
Name
Z[0]
Z[1]
Z[2]
Z[3]
Z[4]
Z[5]
P1
P2
f[0].x
f[0].y
f[1].x
f[1].y
a
b
c
x
y
Address
12
16
20
24
28
32
36
40
44
48
52
56
100
104
108
112
116
120
124
Memory Content/ Value
3
4
12
38
17
9
28
36
25
48
48
36
8
13
Step 13:
printf("%d %d %d %d %d %d ", z[3],z[4],f[0].y,f[1].y);
38 17 2 25
OUTPUT
12 7 15 15
38 17 2 25
Name
z[0]
z[1]
z[2]
z[3]
z[4]
z[5]
P1
P2
f[0].x
f[0].y
f[1].x
f[1].y
a
b
c
x
y
Address
12
16
20
24
28
32
36
40
44
48
52
56
100
104
108
112
116
120
124
Memory Content/ Value
3
4
5
7
2
9
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.