Question: 2. (40 pts) You need to understand array, pointer and pointer arithmetic to complete this exercise. And you should be able to figure out the

2. (40 pts) You need to understand array, pointer and pointer arithmetic to complete this exercise. And you should be able to figure out the answers without actually compiling and running the following C program.

#include

int main(int argc, char *argv[]) {

char a, *pc, c[9];

int i, *pk, k[9];

a='z';

pc=&(c[8]);

pk=&(k[0]);

for (i=0; i<9; i++) {

*pc=a-(char)i;

pc--;

*pk=(int)a-i;

pk++;

}

return 0;

}//end of main

  1. Write out the memory map for the above C program in the following table. For array variables, list the address range for the entire array. Assume the memory address starts from 100, that is, the address for a is 100. Suppose sizeof(char)=1, sizeof(int)=4 and the size of a memory address (a pointer) is 8 bytes. You may assume there is no gap in memory between the variables. (18 pts)

Variable

Start address

End address

a

pc

c

i

pk

k

  1. Show values of the variables in the following table at the end of execution of the for loop before the main function returns. (For the two array variables c and k, list the contents of all array elements.) (18 pts)

Variable

Value

a

pc

Use an expression involving c to represent pcs value

c

list the contents of the entire array

i

pk

Use an expression involving k to represent pks value

k

list the contents of the entire array

  1. True or false? pc and pk have the same value just before return. (2 pts)
  2. True or false? Not taking order of the elements into consideration, the two array c and k contain the same integral values just before return. (2 pts)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!