Practice mode C++

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.