LearnFrenzy
Sign in Take a free test

Arrays · Find the Error · Question 1

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

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

The program below has a problem. Which statement correctly describes it?

main.c
#include <stdio.h>

int main(void) {
    int a[3] = {1, 2, 3}, b[3];
    b = a;
    printf("%d\n", b[0]);
    return 0;
}
  • A b = a; – an array cannot be assigned; copy with a loop or memcpy() Correct
  • B b must be initialised before use
  • C There is no error; it prints 1
  • D b[0] should be written as *b
Correct Answer: Option A

An array name is not a modifiable lvalue, so it cannot be on the left of =. Use memcpy(b, a, sizeof a); (from <string.h>) or a loop.

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.

Popular