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: 
The compiler will substitute a literal value when it is assigned to a `const int`. Effectively your program looks like:

```
        cout<<10<<endl;
        cout<<*p<<endl;

```
As of the main question, it has already been answered: modifying a constant object is undefined behavior.

For the second question, consider that if it was allowed you could break const-correctness inadvertidly:

```
const int k = 10;
void f(int const ** p) {
    *p = &k;                   // Valid, p promises not to change k
}
void g(int const * const * p);

int main()
{
    int *p;
    int ** pp = &p;
    f(pp);                     // If allowed: p points to k!!!
    p = 5;                     // Modifying a constant object!!!
    g(pp);                     //OK
}

```

In the case of `g`, because the signature of the function promises that the intermediate pointer will not be changed, the compiler knows that `g` will not modify `*pp`, which means that it cannot make `*pp` (i.e. `p`) point to a constant object, and const-correctness is guaranteed.