Member Avatar for I_m_rude

hi,

#include<stdio.h>


int main()
{
    const int i=10;
    int *ptr= (int*)&i;
    printf("%d",*ptr);

    *ptr=11;
     printf("%d",*ptr);
     printf("%d",i);
    getch();


    return 0;
}

in this, we trickly changed the value of a constant variable. how is it changing the value of i ? in the o/p it is giving o/p , value of i as 11 after changing it. it has confused me. please comment on this.

thanks in advance.

const doesn't mean that the object is stored in read-only memory, though that's a possibility. All const means is that the compiler will enforce read-only uses of the variable. So you can attempt to subvert the type system by casting away const and it'll probably work, but there's no guarantee.

Member Avatar for I_m_rude

@decptikon sir, So that's mean that this code will work "sometimes" but not "always" ? As you said that "there is no guarantee" ?

So that's mean that this code will work "sometimes" but not "always" ?

It means it might work, or it might not. You've broken the type system and invoked undefined behavior. All bets are off.

Yes that's pretty much it. Good practice would be to treat something that has been declared as const as const. On most of the embedded platforms I have programmed on anything declared const was put into read only memory so assignments wouldn't have worked, you can never be 100% sure your code wont work it's way onto a platform that can do this.

The basic principle is if you need to change the value it isn't constant so don't declare it as such.

Member Avatar for I_m_rude

No, actually some asked me this question when i was going to college in bus. I gave asnwer in negation that no! it will not compile or if it complies, it will crash as you cant increment the value of a constant variable. but when i run it on my syatem, it is working fine(no errors, no warnings).

Member Avatar for I_m_rude

So, it's correct answer is that "the behaviour is undefined". So, finally am i right if i say this thing for this code? thanks in advance. waitinf for precious reply.

Yes, I double checked and it's definitely undefined behavior to modify a const-qualified object.

Member Avatar for I_m_rude

sir, Can you tell me one thing that whenever I am asked that "is the code has any error?" .Then, I probably guess the error. But, I can't tell what type of error it is. Like, it is run time eror, or complitaion time error or it will give warning or it will be undefined behaviour.

please solve this problem.
thanks in advance.

To answer that kind of question, you need to understand the nature of the problem as well as how it manifests. That's a matter of experience.

Member Avatar for I_m_rude

yes, But can I get some link or something from where I can get basic compilation errors , run time errors ? I mean some kind of list so that I can get a over view of type of errors.
thanks.

I'm not aware of any list like that. Maybe you could compile one as you learn C. ;)

Member Avatar for I_m_rude

hahah.. yes! I will definitely and will be in name of daniweb only. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.