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