C++ 数组指针累加后的偏移量

typedef struct _MyStruct
{
	int a;
	int b;
	int c;
	_MyStruct() { a = 0; b = 0; c = 0; }
}MyStruct;

int main()
{
#if 1
	MyStruct* st[3];		//指针数组
	MyStruct* str = new MyStruct;
	st[0] = str;

	cout << st << endl;        //0x00D3F7BC
	cout << st + 1 << endl;      //0x00D3F7C0    //偏移指针长度 4
	cout << (*st) << endl;     //0x01326328
	cout << (*st) + 1 << endl;   //0x01326334    //偏移数组长度 1*sizeof(MyStruct)

	int size = sizeof(st);          //12        //sizeof(MyStruct)
	int size2 = sizeof(st + 1);     //4
	int size3 = sizeof((*st));      //4
	int size4 = sizeof((*st) + 1);  //4
#endif
#if 1
	MyStruct st[2][3];		//二维数组
	MyStruct* str = new MyStruct;
	st[0][0] = *str;

	cout << st << endl;           //0x001BF9AC
	cout << st + 1 << endl;         //0x001BF9D0    //偏移 3*sizeof(MyStruct)
	cout << (*st) << endl;        //0x001BF9AC
	cout << (*st) + 1 << endl;      //0x001BF9B8    //偏移 1*sizeof(MyStruct)

	int size = sizeof(st);          //72    //2*3*sizeof(MyStruct)
	int size2 = sizeof(st + 1);     //4
	int size3 = sizeof((*st));      //36    //3*sizeof(MyStruct)
	int size4 = sizeof((*st) + 1);  //4
#endif

        return 0;
}
posted @ 2022-09-26 12:48  Brickert  Views(101)  Comments(0Edit  收藏  举报