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;
#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.
Comments
Leave a comment