LearnFrenzy
Sign in Take a free test

Arrays and Vectors · Predict the Output · Question 3

Array name used as a pointer Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 3 Back to Exercise

What is the output of the following C++ program on a 64-bit system?

main.cpp
#include <iostream>

int main() {
    int a[] = {10, 20, 30, 40, 50};
    int* p = a + 1;
    std::cout << *p << ' ' << p[2] << ' ' << *(a + 4) << ' ' << 3[a] << ' ' << (p - a) << '\n';
    return 0;
}
  • A 20 40 50 40 1 Correct
  • B 20 30 50 40 1
  • C 10 30 50 40 1
  • D 20 40 50 30 4
Correct Answer: Option A

p points to a[1]; p[2] is a[3] = 40; *(a + 4) = a[4]; 3[a] means *(3 + a) = a[3]. p − a counts elements, not bytes: 1.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links