C++取消引用指针.为什么会发生变化
C++ Dereferencing Pointer. Why is this changing?
我有点难以理解为什么将*放在括号外最终会更改值。我理解为什么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])
相关文章:
- C++取消引用指针.为什么会发生变化
- 为什么在读取文件大小时文件IO速度会发生变化
- 为什么cudaMemGetInfo报告设备内存总量的变化
- 为什么向量内部的指针在从函数返回时会发生变化?
- 为什么 C++ 中的零会发生变化
- 为什么我的全局变量似乎没有变化?
- 为什么预减量会导致奇怪的变化?
- 为什么使用默认构造函数"{}"而不是"= default"存在性能变化?
- 为什么存储在内存位置的值会发生变化?
- 为什么矢量的变化没有反映在 2D 矢量中?
- 为什么C++中的表达式类型在不同版本之间会发生变化
- 为什么尽管源代码没有变化,但每个系统的片段数量却有很大差异?
- 为什么我的 int 在 C++ 程序中间发生了变化?
- 为什么我的程序输出不断变化?EAX寄存器不断更改值
- 为什么认为参考文献在C 底漆第5个中没有变化,以下代码有效
- 为什么在Windows和Mac OS中运行UnionFind时存在广泛的运行时变化?
- 为什么值会发生变化,尽管函数被定义为常量
- 为什么添加内联程序集注释会导致 GCC 生成的代码发生如此根本的变化?
- 为什么数据类型的大小会随着操作系统的变化而变化
- 为什么我的向量在通过构造函数传递后会发生变化