LearnFrenzy
Sign in Take a free test

Arrays and Vectors · Find the Error · Question 2

Error: assigning one C array to another Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 2 Back to Exercise

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links