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 8899263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:48:59+00:00 2026-06-15T00:48:59+00:00

Following is my code const int i = 10; cout<<i<<\n; int *ip = (int*)&i;

  • 0

Following is my code

const int i = 10;
cout<<i<<"\n";
int *ip = (int*)&i;
*ip = 20;
cout<<i<<"\n";

I was expecting output as 10 and 20.
But I am getting output as 10 and 10.

I am able to compile the program and I don’t get any error at compile time or runtime but not getting the expected outcome. If I have removed the constness of the variable i why the new value is not assigned to it?

I would be grateful If somebody could explain whats happening and what can be done to achieve my expected result.

  • 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-06-15T00:49:00+00:00Added an answer on June 15, 2026 at 12:49 am

    How to change the constness of a variable in C++?

    You should never change the constness of an inherently const qualified variable!
    An const qualified variable is declared with the aim that it should not be modified after it is initialized. By attempting to change the constness you break the contract with the compiler.

    If you need to modify a variable, simply do not declare it as const.

    Note that C++ provides const_cast to remove or add constness to a variable. But, while removing constness it should be used to remove constness off a reference/pointer to something that was not originally constant.


    What is happening?

    What is happening is Undefined Behavior.

    You should not be changing the constness of an const qualified variable, If you do so through some pointer hackery what you get is Undefined behavior.

    In case of undefined behavior the standard does not mandate compilers to provide a compile time diagnostic.Note though that some compilers may warn you if you compile with highest warning level.

    Also, undefined behavior means that when you run the program anything can happen and the compiler might show you any result and it does not warrant an explanation.


    Why the result?

    In this case since i is constant probably the compiler inlines the const variable as a part of its optimization and hence the pointer hackery does not affect the value of i since it already inlined by compiler it simply puts the inlined value wherever i is encountered in the code.

    Note that the compiler does so because you made an contract with the compiler,

    Heres my i and it will never be changed throughout the lifetime of my program.

    The compiler is free to apply whatever optimizations it can want as long as it adheres to the contract and it does so in this case.

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

Sidebar

Related Questions

So...I have the following code: int main(void) { const char *s=hello, world; cout<<&s[7]<<endl; return
I have the following code : const int k=1; int *p=const_cast<int *>( &k); cout<<k
consider the following code: const QString& MyClass::getID(int index) const { if (i < myArraySize
Check the following code: string toLowerCase(const string& str) { string res(str); int i; for
I have the following code: class Sales_item { public: int ii; Sales_item& operator=(const Sales_item
I'm a bit confused what happened in the following code: const int e =
i have the following code below: const char* timeformat = %Y-%m-%d %H:%M:%S; const int
The following code #include <iostream> using namespace std; int main() { const char* const
I have the following piece of code compiling under gcc: int parseMsg(const char *msg_to_parse,
Consider the following code: struct Foo { Foo operator+(const Foo &rhs) const; // notice

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.