test_const.c
1 /*
2 * this program is used to test whether the value of a "const" variable can be 100% unmodified.
3 */
4
5 #include<stdio.h>
6 int main(void )
7 {
8 int const a=10;
9 printf("%d\n",a);
10 int *p=(int *)&a;
11 printf("%d\n",*p);
12 *p=100;
13 printf("%d\n",*p);
14 printf("%d\n",a);
15 return 0;
16 }
打印出来的结果是:
10
10
100
100
这就说明了C语言中const修饰的变量的值并非100%不能被修改。在C语言中,const机制是通过编译器检查实现的,它标记const变量不能被直接修改,但并未限制const变量的地址的引用,而只要变量的地址存在被引用的可能,就说明该变量是可以通过指针被间接修改的。所以,只要我们能保证程序在编译过程不出错,那么在程序的运行过程中我们便可以通过指针间接修改该const修饰的变量的值。