C++ 奇怪的运算符[]行为
当我写程序时,我输入了一个错误。我写的是C++ 奇怪的运算符[]行为,c++,operators,C++,Operators,当我写程序时,我输入了一个错误。我写的是I[data],而不是data[I]。然而,该程序编译成功,运行正常 运算符[]与数组的行为: #include <iostream> using namespace std; int main() { int data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; cout << data[6] << endl; // prints 6 cout <<
I[data]
,而不是data[I]
。然而,该程序编译成功,运行正常
运算符[]与数组的行为:
#include <iostream>
using namespace std;
int main()
{
int data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
cout << data[6] << endl; // prints 6
cout << 6[data]; // prints 6
return 0;
}
#包括
使用名称空间std;
int main()
{
int data[]={0,1,2,3,4,5,6,7,8,9,10};
couti[数据]
与*(i+数据)
data[i]
与*(data+i)
而data+i
等于i+data
#include <iostream>
using namespace std;
int main()
{
char* str = "Hello, world!";
cout << str[9] << endl; //prints 'r'
cout << 9[str]; //prints 'r'
return 0;
}