(in c++) What will this program print? EXPLAIN every line. #include using namesp
ID: 3675805 • Letter: #
Question
(in c++)
What will this program print? EXPLAIN every line.
#include
using namespace std;
int main()
{
int a[ ] = { 0, 1, 2, 3, 4} ;
int *p[ ] = { a, a+1, a+2, a+3, a+4 } ;
int **pp = p;
cout << a << " " << *a << endl;
cout << p << " " << *p << " " << **p << endl;
cout << pp << " " << *pp << " " << **pp << endl;
pp++;
cout << pp-p << " " << *pp-a << " " << **pp << endl;
*pp++;
cout << pp-p << " " << *pp-a << " " << **pp << endl;
*++pp;
cout << pp-p << " " << *pp-a << " " << **pp << endl;
++*pp;
cout << pp-p << " " << *pp-a << " " << **pp << endl;
pp = p;
**pp++;
cout << pp-p << " " << *pp-a << " " << **pp << endl;
*++*pp;
cout << pp-p << " " << *pp-a << " " << **pp << endl;
++**pp;
cout << pp-p << " " << *pp-a << " " << **pp << endl;
return 0;
}
Explanation / Answer
#include <iostream>
using namespace std;
using namespace std;
int main()
{
int a[ ] = { 0, 1, 2, 3, 4} ;
int *p[ ] = { a, a+1, a+2, a+3, a+4 } ;
int **pp = p;
cout << a << " " << *a << endl; //Address of a and value of a[0]
cout << p << " " << *p << " " << **p << endl; //address of p , address of a , value of a[0]
cout << pp << " " << *pp << " " << **pp << endl; //address of p , address of a , value of a[0]
pp++;
cout << pp-p << " " << *pp-a << " " << **pp << endl; // pp will point to a+1 and p is poiting a so output will be 1 , and respectively 1 ,1
*pp++;
cout << pp-p << " " << *pp-a << " " << **pp << endl; // pp will point to a+2 and p is poiting a so output will be 2 , and respectively 2 ,2
*++pp;
cout << pp-p << " " << *pp-a << " " << **pp << endl; // same as above
++*pp;
cout << pp-p << " " << *pp-a << " " << **pp << endl; // Here pp is not incremented but *pp
pp = p;
**pp++;
cout << pp-p << " " << *pp-a << " " << **pp << endl;
*++*pp;
cout << pp-p << " " << *pp-a << " " << **pp << endl;
++**pp;
cout << pp-p << " " << *pp-a << " " << **pp << endl;
return 0;
}
0xbfaba1d8 0
0xbfaba1ec 0xbfaba1d8 0
0xbfaba1ec 0xbfaba1d8 0
1 1 1
2 2 2
3 3 3
3 4 4
1 1 1
1 2 2
1 2 3
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.