Question 2
Back to Exercise
The program below does not compile. Which statement correctly describes the problem?
#include <iostream>
int main() {
int a[3] = {1, 2, 3};
int b[3];
b = a;
std::cout << b[0] << '\n';
return 0;
}
- A b must be initialised
- B C arrays cannot be assigned as a whole; copy element by element, use std::copy, or use std::array, which supports = Correct
- C a must be const
- D Arrays of size 3 are not allowed
Correct Answer: Option B
std::array<int, 3> a{1, 2, 3}; auto b = a; works because std::array is a copyable struct.
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!