C++取消引用指针.为什么会发生变化

C++ Dereferencing Pointer. Why is this changing?

本文关键字:变化 为什么 取消 引用 指针 C++      更新时间:2023-10-16

我有点难以理解为什么将*放在括号外最终会更改值。我理解为什么2个打印出来,但我不明白为什么3个打印出来。任何帮助都将不胜感激,谢谢。

int main()
{
//delcaring typedef of boxes
typedef int boxes[2][2];
//delcaring variables that are going to be placed into the boxes
int a=1,b=2,c=3,d=4,e=5,f=6,g=7,h=8;
//declaring two boxes variables and storing variables
boxes myBox={{a,b},{c,d}};
boxes myBox2={{e,f},{g,h}};
//placing those boxes into a pointer boxes array
boxes *x[2][2]={{&myBox,&myBox2,},{&myBox,&myBox2}};
//testing code
cout<<(*x[0][0])[0][1]<<endl;  //prints out 2
cout<<*(x[0][0])[0][1]<<endl;  //prints out 3
}

在处理这类问题中的大量乘法和圆括号时,考虑运算符优先级很重要。它输出3的原因是因为你的程序读取

*(x[0][0])[0][1]

作为

*((x[0][0])[0][1])

因此,您可以看到,在这个过程中,它取消了对整个事物的引用,而不仅仅是(x[0][0])