The following code shows different output with gcc and g++ on using const
variable i
.
The addresses of i
and value of ptr
is same, but on accessing that address by printing value of i
and derefrencing value of ptr
I got value of i
as 5
with g++ and 10
with gcc.
How g++ holds const variable in memory?
#include <stdio.h>
int main()
{
const int i =5;
int *ptr =(int*)&i;
*ptr = 10;
printf("
%u and %u and %d and %d
",&i,ptr,i,*ptr);
return 0;
}
See Question&Answers more detail:os