Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6816851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:58:32+00:00 2026-05-26T20:58:32+00:00

I was asked how can a value of a const variable can be changed.

  • 0

I was asked how can a value of a const variable can be changed.

My my obvious answer was “pointers!” but I tried the next piece of code and I’m puzzled…

int main()
{
    const int x = 5;
    int *ptr = (int *)(&x); // "Cast away" the const-ness..
    cout << "Value at " << ptr << ":"<< (*ptr) <<endl;
    *ptr = 6;
    cout << "Now the value of "<< ptr << " is: " << (*ptr) <<endl;
    cout << "But the value of x is still " << x <<endl;
    return 0;
}

And the output was:

Value at <some address> :5
Now the value of <same address> is: 6
But the value of x is still 5

Now, I’m not sure exactly what is returned from ‘&x’ but it’s definitely not the actual address of x, since the value at x wasn’t changed!

But on the over hand, ptr did contain the value of x at the beginning!
So, what is it exactly?

EDIT compiled with VS2010

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:58:32+00:00Added an answer on May 26, 2026 at 8:58 pm

    Your program invokes undefined behavior (writing to a const variable through a pointer is undefined behavior), so anything might happen. That being said here’s the most likely explanation why you get the behavior you see on your particular implementation:

    When you do &x, you do get the address of x. When you do *ptr = 6, you do write 6 to x‘s memory location. However when you do cout << x, you don’t actually read from x‘s memory location because your compiler optimized the code by replacing x with 5 here. Since x is const the compiler is allowed to do that since there is no legal C++ program in which doing so would change the program’s behavior.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web page where some information are asked. But some value can
I can enumerate many features of functional programming, but when my friend asked me
What is a parametric value? A user has asked me if I can add
A similar question was asked elsewhere, but the answer doesn't seem to work in
I asked a similar question to this already, but I still can't see the
My client has asked me to code the below UI, the value of the
I know this question has been asked/answered but I can't find it for the
This question is asked many times in SO. But still I can't get stuff.
Ok for sure this has been asked and answered already but i somehow can't
I apologize if this has been asked but I can't seem to find it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.