Answer to Question #316566 in C++ for Talat

Question #316566

1) What is the output produced by the following code? Explain the code in detail.

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;

How would the output change if you were to replace *p1 = 30; with the following? *p2 = 30;


1
Expert's answer
2022-03-23T07:17:13-0400

#include <iostream>

using namespace std;


int main() {   

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;

   return 0;

}



If you replace *p1 = 30; with *p2 = 30;,   the output would be the same.


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog