Question
2. (40 pts) Programs must run in Ubuntu. You need to understand array, pointer and pointer arithmetic to complete this exercise. And you should be
2. (40 pts) Programs must run in Ubuntu. 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
- 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 | 100 | 100 |
pc | 101 | 109 |
c | 108 | 108 |
i | 110 | 113 |
pk | 114 | 117 |
k | 114 | 150 Because 114+(9*4) |
- 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 |
- True or false? pc and pk have the same value just before return. (2 pts)
- 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started