I wanted to change value of a constant by using pointers.
Consider the following code
int main()
{
const int const_val = 10;
int *ptr_to_const = &const_val;
printf("Value of constant is %d",const_val);
*ptr_to_const = 20;
printf("Value of constant is %d",const_val);
return 0;
}
As expected the value of constant is modified.
but when I tried the same code with a global constant, I am getting following run time error. The Windows crash reporter is opening. The executable is halting after printing the first printf statement in this statement "*ptr_to_const = 20;"
Consider the following code
const int const_val = 10;
int main()
{
int *ptr_to_const = &const_val;
printf("Value of constant is %d",const_val);
*ptr_to_const = 20;
printf("Value of constant is %d",const_val);
return 0;
}
This program is compiled in mingw environment with codeblocks IDE.
Can anyone explain what is going on?