变量声明顺序和指针偏移问题示例

#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
}
posted @ 2020-09-28 16:23  艾孜尔江  阅读(113)  评论(0编辑  收藏  举报