The answer of this question depends on the type of const variable whether it is local or global const variable.
Const value :- U can't change the value using the variable.
local cons :- U can change the value using its pointer pointing to it.
global const :- U can't change its value using its pointer pointing to it too.
If we go through the in depth of importance of const they we can understand it better.
Consider two example of it where one const variable is local and another is global.
Local const importance: consider this program
/* program 1 */
int main()
{
const int a = 20; // Local const variable
int const *p = &a; // As p is a pointer to const variable
a = 30; //giving compilation error error as it is a const variable.
*p = 30;//No error and value of a also changing ???
}
1> General variables value we can change at different places in the same function or in different function by passing it as argument.
2> Suppose we want to take a variable whose value we don't want to change throughout the program Suppose take a local variable like :- int a = 30;
3> We can take care of it by not changing the value anywhere (By not writing a statement which changes the value of a variable from 30 )
4> So we need to check each and every place like anywhere I have written statement to increase the value of a (e.g a++, a = something ,++a like this) it means that we need to check/review it manually.
5> To not to check manually and giving this task to compiler so that he can check for it somewhere those kind of statement if u want to make a variable whose value will not change till end of program.
6> So for this we can make this value as const and define like this const int a = 30;
so that while compiling compiler will see any statement written which uses x and trying to increment it he will throw a compilation error.
in your example 1 it gives error
a = 30; //giving compilation error error as it is a const variable.
7> But using its address we can changes as the value is changed by considering its address at run time and a is not stored in read only memory also.(As a is local variable stored in stack)
int const *p = &a;
as a is stored in stack so that u can change value of it by accessing its address.
Global const Importance
consider this example
/* program 2 */
const int a = 20; //Global const variable
int main()
{
int const *p = &a; // As p is a pointer to global const variable
a = 30; //giving compilation error as it is a const variable.
*p = 30;//No compilation error but gives segmentation fault ???
}
1> const variable can be global too as you have shown in 2nd example.
2> global const variable is stored in read only memory.
3> as its const variable so we can't write statement like a++, ++a, a= something; it will compilation error as the rule applies the same as local const variable.
4> But when we are accessing it using address and trying to change it (Run time) it gives segmentation fault as the a memory location is at read only data segment area.
5> So we can't write like this (As we can;t change the read only data segment)
Hope it is clear.
References:
http://embeddedgurus.com/barr-code/2012/01/combining-cs-volatile-and-const-keywords/
http://blog.techveda.org/using-const-keyword-in-c/
http://www.noxeos.com/2011/07/29/c-const-static-keywords/