关于C++和C中const的问题
#include <iostream>using namespace std;
int main()
{
const int i = 10;
int * p =(int *) &i;
*p = 20;
cout<<"*p address:"<<p<<endl;
cout<<"i address:"<<&i<<endl;
cout<<"i :"<<i<<endl;
cout<<"*p:"<<*p<<endl;
return 0;
}
运行结果:
*p address:0xbff4b968
i address:0xbff4b968
i :10
*p:20
上述代码用G++编译
#include <stdio.h>
int main( void )
{
const int a = 10;
int *p = (int*)(&a);
*p = 100;
printf("a address: %#p", &a );
printf("*p address: %#p", p );
printf("a value: %d", a );
printf("*p value: %d", *p);
return 0;
}
用gcc编译
结果为:
a address: 0x0022ff74
*p address:0x0022ff74
a value: 100
*p value: 100
为什么会有这种结果呢??先大家谈论 我晚上在发答案~~~~