LearnFrenzy
Sign in Take a free test

Arrays and Vectors · Predict the Output · Question 1

sizeof of an array and of an array parameter Answer, explanation & discussion

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

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

main.cpp
#include <iostream>

void f(int a[10]) { std::cout << sizeof(a) << ' '; }

int main() {
    int a[10];
    std::cout << sizeof(a) << ' ';
    f(a);
    std::cout << sizeof(a) / sizeof(a[0]) << '\n';
    return 0;
}
  • A 40 8 10 Correct
  • B 40 40 10
  • C 10 10 10
  • D 40 4 10
Correct Answer: Option A

In main, a is a real array of 10 ints (40 bytes). The parameter int a[10] is adjusted to int*, so sizeof gives the pointer size, 8. The 10 in the parameter is ignored.

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