I came across this piece of code in which I was able to modify the value of an const int
variable! But is this a bug or a hack? Please clarify me:
#include<stdio.h>
int main(void)
{
const int a = 10;
int* ptr = &a;
printf("\n value at ptr is : [%d]\n",*ptr);
printf("\n Address pointed by ptr : [%p]\n",(unsigned int*)ptr);
*ptr = 11;
printf("\n value at ptr is : [%d]\n",*ptr);
printf("\n %d",a);
return 0;
}
Output:
value at ptr is : [10]
Address pointed by ptr : [0xbf9e614c]
value at ptr is : [11]
11