New Home Work 01 Key
New Home Work 01 Key
(1)
Let the following program
int n = 5;
char *pv, *ppv;
pv = (char *) malloc(n * sizeof(char));
pv[0] = 'A;'
pv[1] = 'E;'
*(pv + 2) = 'I;'
pv[3] = 'O;'
*(pv + 4) = 'U;'
for (ppv = pv+4; *ppv; ppv--)
print(*ppv);
free(pv);
Transform the program above from using only the subscript notation (without using pointers).
char pv[5];
int i;
pv[0] = 'A;'
pv[1] = 'E;'
pv[2] = 'I;'
pv[3] = 'O;'
pv[4] = 'U;'
for (i=4; i>=0; i--)
print(pv[i]);
(3)
024
024
024
420
(4)
P1 P2
A B C
P1 ref to *P1 P2 ref to *P2
Initialization 10 15 20 null X null X
P1=&A 10 15 20 A 10 null X
P2=&C; 10 15 20 A 10 C 20
*P1=(*P2)++; 20 15 21 A 20 C 21
P1=P2; 20 15 21 C 21 C 21
P2=&B; 20 15 21 C 21 B 15
*P1-=*P2; 20 15 6 C 6 B 15
*P1*=*P2; 20 15 90 C 90 B 15
A=(*P2)++ * *P1; 1350 16 90 C 90 B 16
P1=&A; 1350 16 90 A 1350 B 16
*P2=*P1/=*P2; 84 84 90 A 84 B 84
A pointer
a) Refers to many variables.
b) Contains the value of a variable.
c) Contains the address of a variable.
d) None of the above.
What is correct equivalence between the next subscript and offset notations
a) t[i]=2; <=> *t+i=2;.
b) t[i]=2; <=> *(t+i)=2;.
c) t[i][j]=2; <=> **t+i+j=2;.
d) t[i][j]=2; <=> *(*t+i+j)=2;.
(6) Transform the following code from subscript notation to offset notation
30 25 20 15 10
void main()
{
int i,j,*pi,*pj,T[5][4],n=5;
pi=&i;
pj=&j;
for(*pi=0 ; *pi<n ; (*pi)++)
for(j=0 ; j<n-1 ; j++)
*(*(t+*pi)+j)=i+*pj;
*pi=0;
while(i<n)
{
j=0;
while(*pj<n-1)
print(T[*pi][(*pj)++]);
print("\n");
int A[] = {12, 23, 34, 45, 56, 67, 78, 89, 90};
int *P;
P = A;
What are the values or addresses provided by the following expression?
Complete the following table for each instruction of the above program.
P1 P2
T[0] T[1] T[2]
P1 ref to *P1 P2 ref to *P2
Initialization 5 10 15 Null -- Null --
Complete the following table for each instruction of the above program.
P1 P2 P3
T[0] T[1] T[2] T[3] P1 ref P2 ref P3 ref
*P1 *P2 *P3
to to to
int T[4]={-15,-10,-5,10} ;
int *P1=T+1 ;
int *P2=T+2 ;
-15 -10 -5 10 T[1] -10 T[2] -5 Null --
int *P3=Null;
for(P3=T; P3<T+3; P3++)
-5 0 5 10 T[1] 0 T[2] 5 T[3] 10
*P3+=10;
Complete the following table for each instruction of the above program.
P1 P2 P3
P1 P2 P3
*T *(T+1) *(T+2) *(T+3) *(T+4)
ref *P1 ref *P2 ref *P3
to to to
int T[5]={-20,-10,0,10,20}
int *P1=T+3 ;
int *P2=T+2 ;
-20 -10 0 10 20 T[3] 10 T[2] 0 T[0] -20
int *P3=T;
for(int i=4; i>=0; i--)
*(T+i)+=*P3++;
-20 -10 0 0 0 T[3] 0 T[2] 0 T[4] 0
4 1 5 2 6 3
Transform the program above from using only the subscript notation (without using pointers).
double t[2][3];
t[0][0]=1.0;
t[0][1]=2.0;
t[0][2]=3.0;
t[1][0]=4.0;
t[1][1]=5.0;
t[1][2]=6.0;
for(int j=0; j<m; j++)
for(int i=n-1; i>=0; i--)
print(t[i][j]);
int *p;
int i;
int k;
i = 42;
k = i;
p = &i;
char c = 'A';
double *p = &c;
Explanation:
p is a pointer of type *double that can’t refer (point) to a variable of type char.
3. Give the value of the left-hand side variable in each assignment statement. Assume the lines are
executed sequentially. Assume the address of the blocks array is 4434.
Value of the left-hand side
Code variable in each assignment
statement
int main()
{
char blocks[3] = {'A','B','C'}; [‘A’|‘B’|‘C’]
char *ptr = &blocks[0]; 4434
char temp; ‘’
temp = blocks[0]; ‘A’
temp = *(blocks + 2); ‘C’
temp = *(ptr + 1); ‘B’
temp = *ptr; ‘A’
ptr = blocks + 1; 4435
temp = *ptr; ‘B’
temp = *(ptr + 1); ‘C’
ptr = blocks; 4434
temp = *++ptr ; ‘B’
temp = ++*ptr; ‘C’
temp = *ptr++; ‘C’
temp = *ptr; ‘C’
return 0;
}