Give the output from this code fragment int p1 p2 p1 new in

Give the output from this code fragment:
int *p1, *p2;
p1 = new int;
p2 = new int;
*p1 = 10;
*p2 = 20;
cout << *p1 << “ “ << *p2 << endl;
*p1 = *p2;
cout << *p1 << “ “ << *p2 << endl;
*p1 = 30;
cout << *p1 << “ “ << *p2 << endl;

Solution

Answer:

Explanation of the provided code in the question is given as,

int *p1, *p2;                   

p1 = new int;                   

p2 = new int;                   

*p1 = 10;                      

*p2 = 20;                      

cout << *p1 <<\" \" << *p2 << endl;

*p1 = *p2;                      

cout << *p1 <<\" \"<< *p2 << endl;

*p1 = 30;                       

cout << *p1 <<\" \" << *p2 << endl;

The final output of the code is,

10 20

20 20

30 20

Give the output from this code fragment: int *p1, *p2; p1 = new int; p2 = new int; *p1 = 10; *p2 = 20; cout << *p1 << “ “ << *p2 << endl

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site