In the example below, 'ptr' points to the constant variable 'local'. Why does modifying 'local' by assigning '*ptr' not change the value of 'local'?
#include <stdio.h>
int main(void)
{
const int local = 10;
int *ptr = (int*) &local;
printf("address of local: %p \n", &local);
printf("value of ptr: %p \n", ptr);
printf("Initial value of local : %d \n", local);
printf("Initial value of *ptr : %d \n", *ptr);
*ptr = 100;
//*((int*)&local) = 1000;
printf("Modified value of local: %d \n", local);
printf("Modified value of *ptr: %d \n", *ptr);
printf("address of local: %p \n", &local);
printf("value of ptr: %p \n", &(*ptr));
return 0;
}
The output:
address of local: 0x7ffd946bd9c4
value of ptr: 0x7ffd946bd9c4
Initial value of local : 10
Initial value of *ptr : 10
Modified value of local: 10
Modified value of *ptr: 100
address of local: 0x7ffd946bd9c4
value of ptr: 0x7ffd946bd9c4