LearnFrenzy
Sign in Take a free test

Structures, Unions, Enums · Find the Error · Question 2

Error: comparing two structs with == Answer, explanation & discussion

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

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

main.c
#include <stdio.h>

struct point {
    int x, y;
};

int main(void) {
    struct point a = {1, 2}, b = {1, 2};
    if (a == b)
        puts("same");
    return 0;
}
  • A a and b must be pointers
  • B C has no == for structures; compare a.x == b.x && a.y == b.y Correct
  • C There is no error; it prints same
  • D The initializers are invalid
Correct Answer: Option B

The compiler rejects == on struct operands. Compare the members, ideally inside a small function such as bool point_eq(struct point a, struct point b).

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