变量声明顺序和指针偏移问题示例
原创
©著作权归作者所有:来自51CTO博客作者已注销的原创作品,请联系作者获取转载授权,否则将追究法律责任
#include<iostream>
using namespace std;
void run();
int main() {
run();
}
void run() {
//int i2 = 555, i1 = 666; //if opened, the result will be 1, which is the space not allocated
int i1= 555 , i2 = 666; //跟声明顺序有关,先声明的变量会先被分配到内存空间
int* ptr = &i1;
cout << &i1 << " : " << &i2 << endl;
ptr--;
int i3 = *ptr;
cout << i3 << endl;//666
}