0
通常我们以这种方式访问数组元素:arrayName[elementID]
。但即使我们使用elementID[arrayName]
它编译并且不会在运行时导致任何错误。这不是逻辑错误吗?任何人都可以解释我这个。我是C++新手。预先感谢您的任何帮助!C++访问数组元素
#include<iostream>
using namespace std;
int main()
{
int arr[4] = {2, 4, 5, 7};
cout << arr[2] << endl; //this is the correct way to use it
cout << 2[arr] << endl; //this gives the same result and does not cause any errors
return 0;
}
'[]'是近似语法糖,用于解除引用的指针添加,并且加法是可交换的。 – dlf 2014-09-26 15:20:15
当要求其他人阅读你的代码时,请在操作符和逗号后面加空格。它使我们更容易帮助。我已经编辑过它。 – Almo 2014-09-26 15:22:39
谢谢!我会牢记:) – 2014-09-26 15:34:51