c++ *stack+1 与 *(stack+1)区别

具体程序如下:
#include
int main()
{
using namespace std;
//定义了double类型的数组
double wages[3] = { 1000.0,2000.0,3000.0 };
//定义short类型的指针
short stack[3] = { 3,2,1 };
//下面两行程序显示了两种不同的首地址的获取方式
//将数组wages的首地址赋给新创建的double类型的指针,wages数组名就代表了首地址
double * pw = wages;
short * ps = &stack[0];
cout << "pw = " << pw
<< ", *pw = " << *pw << endl;
pw = pw + 1;
cout << “add 1 to the pw pointer\n”;
cout << "pw = " << pw
<< ", *pw = " << *pw << “\n\n”;
cout << "ps = " << ps
<< ", *ps = " << *ps << endl;
ps = ps + 1;
cout << “add 1 to the ps pointer\n”;
cout << "ps = " << ps
<< ",*ps = " << *ps << “\n\n”;
cout << “access two elements with array notation” << endl;
cout << "stacks [0] = " << stack[0] << ", stacks[1] = " << stack[1] << endl;
cout << “access two elements with pointer notation” << endl;
cout << “*stack = " << stack << ",(stack + 1 ) = " << *(stack + 1) << endl;
cout << “*stack+1 = " << *stack + 1 << endl;
cout << sizeof(wages) << " = size of wages array”;
cout << sizeof(pw) << " = size of pw pointer”;

}

在此程序中*(stack +1) 和 *stack+1有什么区别呢?
首先来看结果的区别:
在这里插入图片描述
*(stack+1)是指将指针变量加1,即它会指向下一个位置 再对其进行 * 运算 则得到的为下一个位置的值
stack+1 是先进行stack 在进行加1 ,*stack 获取的为stack数组第一个元素的值3 ,再对3加1得4

*(stack+1)等价于 stack[1]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值