Find the output of the following C++ code: int A[] = {20, 90, 70, 10}; int * P…
2017
Find the output of the following C++ code:
int A[] = {20, 90, 70, 10};
int * P = A;
A[2] += 10;
P += 2;
cout << *P << endl;
- A.
70
- B.
80
- C.
90
- D.
10
Attempted by 186 students.
Show answer & explanation
Correct answer: B
Correct Answer: 80
Reasoning:
Interpretation: P is intended to be a pointer to int (int *P). P = A makes P point to the first element of the array A.
A[2] starts as 70; the statement A[2] += 10 increases it to 80.
P += 2 advances the pointer so it points to A[2].
Dereferencing the pointer (*P) then yields 80, which is printed.
Note: The original code snippet appears to omit the pointer asterisk and the dereference in the print statement. For this result, the declarations and output should be int *P, A[] = {20, 90, 70, 10}; P = A; A[2] += 10; P += 2; cout << *P << endl;.
A video solution is available for this question — log in and enroll to watch it.