指针操作(指针类型和指针类型转换为其他类型时侧操作)

当为指针类型时,将指针直接+1即可指向指针的下一个地址,例如p,p+1指向p的下一个地址。当指针类型转换为int类型时,需要+4(32位系统指针占用4个字节)可以指向指针的下一个地址,例如p,(int)p + 4指向p的下一个地址。


```cpp
#include<iostream>

using namespace std;

class Father {
public:
public:
	int x = 200;
	int y = 300;
};

int main(void) {
	Father father;
	int * vptr = (int*)&father;										//对象的地址

	cout << "对象的地址:" << endl;
	cout << hex << vptr << endl;

	cout << "第一个数据成员的地址:" << endl;
	cout << &father.x << endl;
	cout << vptr + 1 << endl;
	cout << (int)&father + 4 << endl;			//必须加4
	cout << "第二个数据成员的地址:" << endl;
	cout << &father.y << endl;
	cout << vptr + 2 << endl;
	cout << (int)&father + 8 << endl;			//第二个必须加8

	cout << "第一个数据成员的值:" << endl;
	cout << dec << father.x << endl;
	cout << *(vptr + 1) << endl;
	cout << *(int *)((int)&father + 4) << endl;
	cout << "第二个数据成员的值:" << endl;
	cout << father.y << endl;
	cout << *(vptr + 2) << endl;
	cout << *(int *)((int)& father + 8) << endl;

	system("pause");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值