include include using stdcout using stdendl int main int in

#include <iostream>

#include <cstdlib>

using std::cout;

using std::endl;

int main() {

int integer1, * p1, ** p2;

integer1 = 10; // line 11

p1 = &integer1; // line 12

p2 = &p1; // line 13

cout << \"integer1 = \" << integer1 <<endl; // line 15

cout << \"p1 = \" << p1 << endl; // line 16

cout << \"p2 = \" << p2 << endl; // line 17

return EXIT_SUCCESS;

} // main

Suppose after the declaration of variables integer1, p1, and p2, we have the following memory map:

(a) Fill in the “Value” column in the in the memory map to reflect the changes that are caused by lines 11, 12, and 13.

(b) If we substitute lines 15–17 with the the following two statements, then what will be the output of the program?

   (*p1)++;

   cout << \"integer1 = \" << *p1 << endl;

(c) Will the output of the program be the same if we substitute the above two lines with the following two statements?

   integer1++;

   cout << \"integer1 = \" << *p1 << endl;

(d) Will the output of the program be the same if we substitute the above two lines with the following two statements?

   *p2++;

   cout << \"integer1 = \" << integer1 << endl;

(e) Explain why the outputs of parts (c) and (d) are the same or different from each other.

Symbol Name Type Memory Address Value integeri int 80 p1 int 84 p2 int 92

Solution

a)

The value of integer1 will be 10 as declared in line 11.

Value of p1 will be the address of integer1, i.e. 80

Value of p2 will be the address of pointer p1, i.e. 84

b) Here we incremented the value pointed to by pointer p1, which is integer1=10

Output will be

integer1 = 11

c) Yes, the output will be same, as we are incrementing the integer1 here directly.

d) No, the output will not be the same, here we are not incrementing any variable, we are incrementing the value pointed to by pointer pointer p2, which is, pointer p1. Now, pointer p1 has the address of integer1 which will be incremented here.

e) Explained in part d)

#include <iostream> #include <cstdlib> using std::cout; using std::endl; int main() { int integer1, * p1, ** p2; integer1 = 10; // line 11 p1 = &
#include <iostream> #include <cstdlib> using std::cout; using std::endl; int main() { int integer1, * p1, ** p2; integer1 = 10; // line 11 p1 = &

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site