1.5k views
5 votes
Given the C++ program, determine the output:

#include
using namespace std;
int main() {
int i, j;
int *pi, *pj;
i = 5;
j = i;
pi = &i;
pj = &j;
*pj = 4;
cout << i << " " << j << " " << *pi << " " << *pj << endl;
return 0;
}

User Foxwendy
by
7.9k points

1 Answer

3 votes

Final answer:

The output of the given C++ program will be '5 4 5 4', representing the separate values of the variables i, j, and the dereferenced values of pointers pi and pj.

Step-by-step explanation:

The given C++ program initializes two integer variables i and j, and two pointer variables pi and pj. Initially, i is set to 5, and j is assigned the value of i. Next, pi is assigned the address of i, and pj is assigned the address of j. When *pj is set to 4, it changes the value of j but not i. Therefore, the output of the program will be '5 4 5 4', corresponding to the values of i, j, the value pointed to by pi (which is i), and the value pointed to by pj (which is j) respectively.

User Ootoovak
by
8.3k points