LearnFrenzy
Sign in Take a free test

C Const · const MCQ · Question 6

Modifying a const object through a cast in C Answer, explanation & discussion

  • MCQ
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 6 Back to Exercise

What is the result of *(int *) &limit = 20; when limit was defined as static const int limit = 10;?

  • A limit becomes 20
  • B Undefined behaviour – the object may be in read-only memory or its value folded into the code Correct
  • C A compile error
  • D limit becomes 0
Correct Answer: Option B

The cast removes the compiler's protection, but the object is still const. Writing to it is undefined: on most systems it sits in read-only memory and the program crashes.

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