Assuming standard headers and using namespace std, what does this C++ code print? int arr = {10, 20, 30, 40}; int* p = arr; p += 2; cout << *p << ” ” << *(p – 1) << ” ” << p;
Correct answer D. 30 20 40
Explanation
The array name decays to a pointer to its first element. Adding 2 moves p forward by two int elements, not two bytes, so *p is 30. *(p - 1) is the element before it, 20, and p[1] is defined as *(p + 1), which is 40. Pointer arithmetic is always scaled by the size of the pointed-to type.
Answer checked How we write and check questions
Discussion