|
|
发表于 2006-10-24 11:43:37
|
显示全部楼层
貌似gcc允许如此只是历史原因,程序不能这么写,结果不可预计
注意
a1.c:4: warning: initialization discards qualifiers from pointer target type
- lucida@fancyworld ~/test $ cat a1.c
- void main(void)
- {
- const int a = 4;
- int *p = &a;
- *p = 5;
- printf("a=%d, *p=%d\n", a, *p);
- }
- lucida@fancyworld ~/test $ gcc a1.c
- a1.c: In function ‘main’:
- a1.c:4: warning: initialization discards qualifiers from pointer target type
- a1.c:6: warning: incompatible implicit declaration of built-in function ‘printf’
- a1.c:2: warning: return type of ‘main’ is not ‘int’
- lucida@fancyworld ~/test $ ./a.out
- a=5, *p=5
- lucida@fancyworld ~/test $ gcc a1.c -O2
- a1.c: In function ‘main’:
- a1.c:4: warning: initialization discards qualifiers from pointer target type
- a1.c:6: warning: incompatible implicit declaration of built-in function ‘printf’
- a1.c:2: warning: return type of ‘main’ is not ‘int’
- lucida@fancyworld ~/test $ ./a.out
- a=4, *p=5
复制代码
g++就很好
- lucida@fancyworld ~/test $ cat a1.cc
- #include <iostream>
- using namespace std;
- int main(void)
- {
- const int a = 4;
- const int* const p = &a;
- *p = 5;
- cout<< "%d\n"<< a<<endl;
- return 0;
- }
- lucida@fancyworld ~/test $ gcc a1.cc
- a1.cc: In function ‘int main()’:
- a1.cc:9: error: assignment of read-only location
复制代码 |
|