1 Compile and run the following program then answer the corr
1. Compile and run the following program, then answer the corresponding questions:
int main() {
(a) What is the type of the iarray variable (Hint: It’s some kind of pointer.)? (b) What is the value of iarray?
(c) If you dereference iarray, what do you get? (d) What is the output of the program?
(e) Will the output change if we replace the first for-loop with the following? Why or why not?
(f) Will the output change if we replace the second for-loop with the following? Why or why not?
(g) Will the output change if we replace the first for-loop with the following? Why or why not?
2. Consider the following code:
} // main
(a) Complete the memory map given below.
(b) Will the program successfully compile if we substitute line 17 with the following statement? Why or why not?
(c) Will the program successfully compile if we substitute line 17 with the following statement? Why or why not?
(d) Will the program successfully compile if we substitute line 25 with the following statement? Why or why not?
Symbol Address Value Meaning of Symbol\'s Type 10 x addr-x an int px addr-px addr-x a pointer to an int a addr-a addr-a Co] an array of 10 ints a[0] addr-a [0] 100 an int a C1] a C21 a[3] a [61 al8 b addr-b addr-b[0] an array of 5 ints b[0] addr-b [0] an int bC21 b[3] [4] a pointer to an int pi p2 parray an array of 3 pointers to ints ap1 api [0] ap1 [1] api [2] an array of 2 pointers to arrays of 10 ints ap2 ap200] ap2011Solution
Hi, I have answered all parts of Q1.
Please repost others in separate post.
Please let me know in case of any doubt in first part.
1. Compile and run the following program, then answer the corresponding questions:
#include <iostream>
#include <cstdlib>
using std::cout;
using std::endl;
int main() {
int iarray [10], * p1, * p2;
p1 = iarray;
p2 = &iarray[0];
for (int i = 0; i < 10; ++i) iarray[i] = i;
for (int i = 0; i < 10; ++i) cout << *(p2 + i) << endl;
return EXIT_SUCCESS;
} // main
(a) What is the type of the iarray variable (Hint: It’s some kind of pointer.)?
iarray is array of type integer. It is a contant pointer.
(b) What is the value of iarray?
\'iarray\' points the base address of iarray. In other word it points the first element of array(iarray[0]).
(c) If you dereference iarray, what do you get? (d) What is the output of the program?
After dereference iarray, you will get value of first element of array (iarray[0]).
Output: 0
(e) Will the output change if we replace the first for-loop with the following? Why or why not?
for (int i = 0; i < 10; ++i) *(p1 + i) = i;
No. It will not change anything.
p1 is pointing to base address(first element) of iarray.
So, adding i=0, 1, 2,3... , it will points first, second, .... respective element of iarray.
so, *(p1+i) = p[i] // same thing
(f) Will the output change if we replace the second for-loop with the following? Why or why not?
for (int i = 0; i < 10; ++i) cout << *(p1 + i) << endl;
No. It will not change anything.
p1 is pointing to base address(first element) of iarray.
So, dereference p1 will give value of iarray[0]
dereference *(p1+1) give value of iarray[1] and so on.
(g) Will the output change if we replace the first for-loop with the following? Why or why not?
for (int i = 0; i < 10; ++i) *(p1++) = i;
No, it will not change anything.
Initially, p1 is pointing to base address(first element) of iarray.
so, dereference p1 gives the value of iarray[0]
So, *(p1++) means dereference current address and then increasing p1 by 1
*(p1++) = i => store i in iarray[0] and increase p1, so it now pints to iarray[1]

