I am a bit puzzled by the following program (found at one of the tutorials on const keyword)
[insert]
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
const int my_age = 24;
printf("\n%d", my_age);
//my_age = 25; Will generate error
printf("\n%p", &my_age);
printf("\n%p", my_age);
*(int *)(&my_age) = 25;
printf("\n%d", my_age);
printf("\n%p", &my_age);
printf("\n%p", my_age);
return 0;
}
Though const prefixed variable cannot be changed but it said that using the pointer notation it could be changed. That is fine. My doubt is that i have my_age equal to some integer value say it is 25. And address of my_age is say 0x000012ff.
Now the notation that these guys use is
*(int *)(&my_age) = 25;
I can understand what it is doing. But when i went i thought that even using the following expression should change the value
*(&my_age) = 25; (Value at address of my_age equals to 25, but it does not work)
Why appending (int *) works ? I can understand that its a typecast or may be because by default (&my_age) returns a (void *).
Any thoughts ????????