This line will be assembled by the compiler something like,
mov dword ptr [a], 64h
So the data is stored in memory.
This will not be possible. The compiler will show error because there
is pointer type mismatch. &a is const int* and nump is int*.
Here you are forcing the casting. Asking the compiler to shut up and
accept. So compiler tells nothing.
works. After that I can change
This will work because the memory handling of a const variable is also
same as ordinary variable.
cout << &a << " : " << a << endl;
Now when you use const variable the compiler dont read it from memory.
Instead like using the #define values compiler replaces the all
occurance of a with the value you assigned to a.
So the source code will be almost like,
cout << &a << " : " << 64h << endl
ie:cout << address of a<<":"<<initial value of a directly<<endl;
so you will get the initial value of a as output.
Remember that the compiler replaces all the existance of a const
variable with the value that you initialized in the compile time. This
almost uses the same logic of #define even though the const variable
is stored in the memory.
cout << nump << " : " << *nump << endl;
Here it will print the modified value because you are accessing the
value that is in the memory. You have modified the data and so if you
look for the value in the memory it will be the modified one. Whereas
compiler guarantees the direct usage of a const variable without using
memory
0012FF60 : 100
0012FF60 : 80
Can same addresses have different value? Or Is there anything else
that I don't know?
Any help will be highly appreciated.
For realizing pls try add following lines.
const int* p;
p = &a;
cout << *p;
This will also print the modified value of a. So the output will be
80. That means the compiler replaces all a's to 64h(100) during the
compilation.
so if you write,
int x = a * 10;
compiler will make it as,
int x = 100 * 10 while compiling.
Thanks and regards,
Amal P.