Question 3
Back to Exercise
The program below has a bug. Which statement correctly describes it?
#include <stdio.h>
struct point {
int x, y;
};
int main(void) {
const struct point origin = {0, 0};
origin.x = 5;
printf("%d\n", origin.x);
return 0;
}
- A Structs cannot be const
- B origin is const, so every member is read-only; copy it into a non-const struct to change it Correct
- C x must be declared const inside the struct
- D There is no bug
Correct Answer: Option B
const applies to the whole object, including all its members. Make a modifiable copy: struct point p = origin; p.x = 5;.
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!