Question: > 
> **Possible Duplicate:**  
> 
> [C/C++ changing the value of a const](https://stackoverflow.com/questions/583076/c-c-changing-the-value-of-a-const) 
> 
> 
> 

The following program:

```
int main()
{
    const int x = 10;
    int * p = (int *)&x;
    *p = 12;
    cout<<x<<endl;
    cout<<*p<<endl;
    return 0;
}

```

Gives an output of:

```
10
12

```

What is the effect of casting &x to (int \*) and why is the value of x still 10 ?
I expected it to be 12.

Another doubt 

Why cant we cast say int \*\* to int const \*\* ?
Rather this operation is valid 

```
void f(int const ** p);
void g(int const * const * p);

int main()
{
    int ** p = /*....*/
    f(p);   //Error
    g(p);   //OK
}

```

Please help me understanding this with suitable example
Thanks !!!

Answer: 
You are invoking undefined behaviour by modifying a variable declared as `const`. Any result whatsoever is legitimate output from the program, though some results are more plausible than others.

As I noted in a comment:

The 10 may be due to optimization; the compiler knows that `x` is const so it may invoke:

```
cout << 10 << endl;

```

instead of:

```
cout << x << endl;

```

What's less clear is whether the compiler optimizes the second output to:

```
cout << 12 << endl;

```

You'd have to look at the assembler generated to know for sure. But since the whole program is invoking undefined behaviour, the compiler is correct, regardless of what it does.
well， it is interesting question.
when you remove the const prefix of x you will get the result as you wish.

```
int main()       
{               
 int x = 10;               
 int * p = (int *)&x;               
 *p = 12;              
 cout<<x<<endl;              
 cout<<*p<<endl;               
 return 0;      
}

```

I think it is the const mark prevent the change of the value of x.