CSC209H Worksheet: Array and Pointer Basics
1. Here is the code of a small program that uses both arrays and pointers. Beside it we have drawn a memory diagram with the stack frame of main.
Use this diagram to trace the execution of the program. When the value stored at a location changes, cross out the old one and write the new one (rather than simply writing the new one). If there are unitialized blocks of memory when main returns, write their values as ???.
int main() {
int i = 2;
int j = 30;
int a[4];
int *p;
int *q;
p = &i;
j = *p;
*p = 1;
a[0] = 10;
a[3] = 12;
a[i] = 11;
return 0;
Section Address
stack frame 0x234 for main
0x238
0x23c o
Value
J
10
12
302
211
l I
Label
q
p
a[0]
j i
I
r
chare
0x240 a 0x244 0x248 0x24c 0x250 0x254 0x258 0x25c
char
}
char bag bDa
int p xP
0x260 lal 0x264
Ip
b
char *result[2];
y
y
array 40 Ed
xd
X
result
x = result[0];
I.mg
// some hidden code
c
chorale
r
d
xtD
lxt2
char x
resulted
char
y
char
CSC209H Worksheet: Array and Pointer Basics
2. Each example below contains an independent code fragment. In each case there are variables x and y that are missing declaration statements. In the boxes to the right of the code write declaration statements so that the code fragment would compile and run without warnings or errors.
Code Fragment
x = 10; y = ¡¯A¡¯;
int age = 99;
x = &age;
y = *x;
double *p;
x = &p;
y = &x;
float f = 4.5;
float *p = &f;
x = &p;
y = **x;
ensuite
result[0] = “read only”;
y = x[0];
x03
h
Declaration for x int x
intitix
double X float X
Declaration for y
chary
inty
double
float
or
char g resulted
readonly infarrfr
X
ed char rested
rest
char
char
result
intx
arreg
x
mainlintargc
f result
rest
X03 e
char argu char argue
int
E
REINS
d
X
0 10 d yw
resulted
Yg
4C 50