Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 奇怪的运算符[]行为_C++_Operators - Fatal编程技术网

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};

cout
i[数据]
*(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;
}