Question 4
Back to Exercise
The program below does not compile. Which statement correctly describes the problem?
#include <iostream>
int[3] make() {
int a[3] = {1, 2, 3};
return a;
}
int main() {
std::cout << make()[0] << '\n';
return 0;
}
- A make must be static
- B Functions cannot return C arrays; return std::array<int, 3> or std::vector<int> instead Correct
- C The array must be initialised with ()
- D main cannot index a call
Correct Answer: Option B
Even returning a pointer to a would be wrong, because the local array dies at the end of the function. std::array<int, 3> make() { return {1, 2, 3}; } is correct.
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!