在此代码中,我可以理解,处理“ a * 2”的注释代码发生错误,因为“ a”是数组的地址。 因此可以对其进行修改。 但是我不明白为什么第32行和第33行会出错。 我认为当我创建另一个名为“ b”的指针变量时,可以修改变量“ b”。但这使我出错。 请告诉我为什么会发生错误....
#include <iostream>
using namespace std;
/*
* This is an example about pointer arithemtic
*/
int main()
{
int a[5];
for (int i = 0; i < 5; i++) {
a[i] = i;
}
// pointer arithemtic for +
for (int i = 0; i < 5; i++) {
cout << a + i << "\n";
}
// pointer arithemtic for -
int *c = a;
for (int i = 1; i < 5; i++) {
cout << a + i - c << "\n";
}
/* Try to compile the folloing region */
// cout << a * 2 << "\n";
// cout << a / 2 << "\n";
//
int *b = a;
cout << b * 2 << "\n";
cout << b / 2 << "\n";
}