Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1In the given program, What statements change the *p1? What statements change th

ID: 3813112 • Letter: 1

Question

1In the given program, What statements change the *p1? What statements change the *p2?

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

#include "stdafx.h"

#include

using namespace std;

int main( )

{

    int *p1, *p2;

    p1 = new int;

    *p1 = 42; // initial value = 42

    p2 = p1; //

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

    cout << "*p2 == " << *p2 << endl;

    *p2 = 53;

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

    cout << "*p2 == " << *p2 << endl;

     

    p1 = new int;

    *p1 = 88;

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

    cout << "*p2 == " << *p2 << endl;

    *p2 = *p1;

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

    cout << "*p2 == " << *p2 << endl;

    cout << "Hope you got the point of this example! ";

    system("pause");

    return 0;

}

1In the given program, What statements change the *p1? What statements change the *p2?

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

#include "stdafx.h"

#include

using namespace std;

int main( )

{

    int *p1, *p2;

    p1 = new int;

    *p1 = 42; // initial value = 42

    p2 = p1; //

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

    cout << "*p2 == " << *p2 << endl;

    *p2 = 53;

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

    cout << "*p2 == " << *p2 << endl;

     

    p1 = new int;

    *p1 = 88;

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

    cout << "*p2 == " << *p2 << endl;

    *p2 = *p1;

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

    cout << "*p2 == " << *p2 << endl;

    cout << "Hope you got the point of this example! ";

    system("pause");

    return 0;

}

Explanation / Answer

#include "stdafx.h"
#include
using namespace std;
int main( )
{
int *p1, *p2;
p1 = new int; // P1 address is changed here
*p1 = 42; // initial value = 42.. assigning to location of p1
p2 = p1; // p1, p2 points to same location now..    // P2 address is changed here
cout << "*p1 == " << *p1 << endl; // prinitng value from p1 location
cout << "*p2 == " << *p2 << endl; // prinitng value from p2 location
  
*p2 = 53; // Now P1/P2 locations value has been changed, no address change
  
   // *p1 and *p2 both will now print 53
cout << "*p1 == " << *p1 << endl;
cout << "*p2 == " << *p2 << endl;

   // now P1 points to a new location.
p1 = new int;   // P1 address is changed here
*p1 = 88; // assigning value 88 to P1
  
   // printing p1 and p2 value, 88 and 53 will be printed
cout << "*p1 == " << *p1 << endl;
cout << "*p2 == " << *p2 << endl;
  
   // now p2 location has same value as p1, loctions still remain diffrent
*p2 = *p1;
  
   // both point to same value.. 88 and 88 will be printed
cout << "*p1 == " << *p1 << endl;
cout << "*p2 == " << *p2 << endl;
  
cout << "Hope you got the point of this example! ";
system("pause");
return 0;
}

I have highlighted above wherever p1 or p2 is getting changed. When we say p1 or p2 is getting changes, we essesntially means the locations to which these pointers are pointing, they are getting changed.. not the values which are contained in the location

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote