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;
}
0x240 a 0x244 0x248 0x24c 0x250 0x254 0x258 0x25c 0x260 0x264
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 ???.
Section Address
Value Label
q
p
a[0]
j i
stack frame for main
0x234
0x238 0x23c
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
Declaration for x
Declaration for y
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;
char *result[2];
x = result[0];
// some hidden code
result[0] = “read only”;
y = x[0];