Thread: Changing constant value by pointer variable

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    34

    Changing constant value by pointer variable

    Hi guys.
    I'm wondering if it's possible that when I declare an constant value and I point to it by pointer variable (not constant pointer variable) ... is it legible and possible to change the value that Im pointing at?

    I mean for example lets assume
    Code:
     const int var=10
    so if I point to this value by integer pointer variable (not constant pointer) will the value that Im pointing at (the value var) change?


    another question in the same context, if I have pointer variable which it's constant (i.e like
    Code:
     int const *ptr
    ) so I can't change the value that ptr is pointing at
    by ptr pointer, but I can declare another pointer variable like int *ptr2 and let it points to the
    same value and then I can change the value by ptr2 pointer .. right?
    in other words
    Code:
     int const *ptr
    is a constant regarding to ptr itself but I can declare another
    pointer variable which it's not const and change the value that ptr pointing at by
    the new pointer variable .. right?
    Last edited by JohnnyOmari; 12-09-2020 at 02:30 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,304
    Quote Originally Posted by JohnnyOmari
    so if I point to this value by integer pointer variable (not constant pointer) will the value that Im pointing at (the value var) change?
    I believe that you will get undefined behaviour. If you compile at a high enough warning level, your compiler should warn you about this though, and while you could suppress that warning with a cast, you'll then be taking responsibility for the potential undefined behaviour. Having said that, such casting does have its uses with legacy APIs that might not be const-correct.

    Quote Originally Posted by JohnnyOmari
    but I can declare another pointer variable like int *ptr2 and let it points to the
    same value and then I can change the value by ptr2 pointer .. right?
    Yes, assuming that what ptr2 points to isn't actually constant, otherwise you risk undefined behaviour. So, it is not advisable to do this: it would be better to change ptr2 to be a pointer to non-const instead, and if you cannot do so, then it is a good hint that perhaps what ptr2 points to might really be constant, so you should not attempt to bypass the const-ness.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about constant variable in c/c++
    By fatshaw in forum C++ Programming
    Replies: 2
    Last Post: 12-16-2010, 10:16 PM
  2. constant being treated as 32-bit when variable is 64-bit
    By ulillillia in forum C Programming
    Replies: 12
    Last Post: 08-13-2010, 12:07 AM
  3. Constant Being Initialized AFTER Variable That Uses It
    By mercury529 in forum C++ Programming
    Replies: 22
    Last Post: 07-30-2006, 09:51 AM
  4. Changing a variable to a constant.
    By esben in forum C Programming
    Replies: 6
    Last Post: 03-22-2006, 06:29 AM
  5. Constant pointer to constant value
    By tretton in forum C Programming
    Replies: 10
    Last Post: 12-23-2005, 01:45 PM

Tags for this Thread