C ++试图了解顺时针规则以解密复杂语法 [英] c++ trying to understand clockwise rules for deciphering complicated syntax

查看:31
本文介绍了C ++试图了解顺时针规则以解密复杂语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

int ia[3][4] = {    //
{0, 1, 2, 3},   //
{4, 5, 6, 7},   //
{8, 9, 10, 11}  //
};

int (*p4)[4] = ia;
cout << "(*(p4 + 0))[3] = " << (*(p4 + 0))[3] << endl;
cout << "*(p4 + 0)[3] = " << *(p4 + 0)[3] << endl;

获得以下输出:

(*(p4 + 0))[3] = 3
*(p4 + 0)[3] = 1

我不明白最后一个如何到达1.任何帮助将是巨大的.谢谢.

I don't understand the last one how it arrives at 1. Any help would be great. Thanks.

推荐答案

我不明白最后一个是如何到达1.

I don't understand the last one how it arrives at 1.

未定义的行为使您到达那里.

Undefined behavior got you there.

由于运算符的优先级(数组索引运算符的绑定比指针解引用运算符更紧密),

Due to operator precedence (the array indexing operator binds tighter than the pointer dereference operator),

*(p4 + 0)[3] 与以下内容相同:
*((p4 + 0)[3]),与以下代码相同:
*(p4 [3]),与:
p4 [3] [0] .

*(p4 + 0)[3] is the same as:
*((p4 + 0)[3]), which is the same as:
*(p4[3]), which is the same as:
p4[3][0].

对于您的阵列.第一个维度的有效索引为: 0 1 2 .使用 3 的索引值访问数组将访问超出有效范围的内存,从而导致未定义的行为.

For your array. the valid indices for the first dimension are: 0, 1, and 2. Accessing the array using the index value of 3 accesses memory beyond valid range, leading to undefined behavior.

这篇关于C ++试图了解顺时针规则以解密复杂语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆