关于const变量

来源:互联网 发布:迅龙数据软件注册码 编辑:程序博客网 时间:2024/06/11 21:52

说明:测试环境dev-c++

最近复习C语言,在复习const变量时发现如下特性。定义const变量a

const int a = 10;

假如 a = 11;编译报错。

但是假如使用指针来访问const变量的内存,则是可以修改a变量的。

如:

#include <stdio.h>#include <stdlib.h>int main(int argc, char *argv[]){    const int a = 10;    int *b;    b = (int *)&a;    printf("%d\n",a);    printf("%d\n",*b);        *b = 11;    printf("%d\n",a);    printf("%d\n",*b);  system("PAUSE");  return 0;}
 

这说明const只能帮助程序员发现无意的修改,并不能保证恶意的修改。