简体   繁体   English

多维数组的算术运算

[英]Arithmetic operation on multidimensional array

I am doing some arithmetic operations on multidimensional array. 我正在对多维数组进行一些算术运算。 Also tried some code using C++. 还使用C ++尝试了一些代码。 But, output of my code contradict with my theoretical understanding on relation between different pointers and multidimensional array. 但是,我的代码输出与我对不同指针与多维数组之间的关系的理论理解相矛盾。

My understanding on 2D and 3D array. 我对2D和3D阵列的理解。

2D Array 2D阵列

1) 1) 在此处输入图片说明 Again, by using dereference operator 同样,通过使用取消引用运算符

2) 2) 在此处输入图片说明

3D Array 3D阵列

3) 3) 在此处输入图片说明 Again, by using dereference operator 同样,通过使用取消引用运算符

4) 4) 在此处输入图片说明

I am confused about what B and C pointing in figure 2 and 4 respectively. 我对B和C分别指向图2和4感到困惑。

Code on 2D array 二维数组上的代码

#include <iostream>
using namespace std;

int main(void){



    int B[2][2] = {1, 2, 3, 4};
    cout << B << endl;
    cout << B+1 << endl;
    return 0;
}

Output: 输出:

0x7ffec3dd9f80 0x7ffec3dd9f80

0x7ffec3dd9f88 0x7ffec3dd9f88

Here, pointer to int B is increased by ((1*4)+(1*4)) = 8 unit that is pointer to int (B+1). 在这里,指向int B的指针增加了((1 * 4)+(1 * 4))= 8个单位,它是指向int(B + 1)的指针。 Though I got one explanation from Link on what actually B points in 2D array. 尽管我从Link那里得到了一个关于B实际指向2D数组的解释。 It points first element of 2D array. 它指向2D数组的第一个元素。

Output of my code and explanation on this Link confused me. 我的代码输出和此链接上的说明使我感到困惑。 What actually B points in 2D array? B在二维数组中实际上指向什么? And, If B points the address of first element of 2D array, then why value of B is increased by 8 unit when we add 1 with it? 并且,如果B指向2D数组的第一个元素的地址,那么为什么当我们加1时B的值增加8个单位? Why not increased by (1*4) unit? 为什么不增加(1 * 4)单位?

Code on 3D array 3D阵列上的代码

#include <iostream>
using namespace std;

int main(void){



    int C[2][2][2] = {{
                    {1, 2},
                    {3, 4}
                  },
                  {
                    {1, 2},
                    {3, 4}
                  }};

    cout << C << endl;
    cout << C[0] << endl;
    cout << C[0] +1 << endl;
    cout << C[0][0] +1 << endl;
    return 0;
}

Output: 输出:

0x7ffeac58d130 0x7ffeac58d130

0x7ffeac58d130 0x7ffeac58d130

0x7ffeac58d138 0x7ffeac58d138

0x7ffeac58d134 0x7ffeac58d134

C gives starting address of whole array. C给出整个数组的起始地址。 But, which portion specifically it points? 但是,它具体指出了哪一部分? Is it pointing &C[0] or &C[0][0] or&C[0][0][0]? 它指向&C [0]或&C [0] [0]还是&C [0] [0] [0]?

cout << C[0]+1 << endl; here, C[0] is increased by 8 unit. But, if C[0] is pointing the base address of 1st 2D array inside 3D array, then it must be increased by 16 unit to get base &C[1].

cout << C[0][0] +1 << endl; here, C[0][0] is increased by 4 unit. But if it is pointing the base address of 1st 1D array of 1st 2D array inside 3D array, then it must be increased by 8 unit to get base &C[0][1].

Thank you. 谢谢。

Diagrams 1 and 3 are correct Diagrams 2 and 4 are not. 图1和3是正确的图2和4不是正确的。 *B and B[0] are defined to mean exactly the same thing. *BB[0]的定义完全相同。 Similarly with **B and B[0][0] . **BB[0][0]

So to fix Diagram 2, actually copy Diagram 1 but merely change the text B[0] to *B and so on. 因此,要修复图2,实际上是复制图1,但只需将文本B[0]更改为*B ,依此类推。 B by itself refers to the whole array. B本身是指整个数组。 Ditto for Diagram 4. 图4的同上。

B+1 is defined as meaning &(B[1]) , it refers to a temporary pointer pointing at the object you have correctly marked as B[1] in Diagram 1. B+1被定义为&(B[1])含义,它表示一个临时指针,该指针指向您在图1中正确标记为B[1]的对象。

In cout << B << endl; cout << B << endl; , treat B as saying B+0 (ie &(B[0]) ). ,将B视为说B+0 (即&(B[0]) )。 When you use an array in a context where a pointer is expected, that is what happens (a temporary pointer is created, it behaves the same as if you had written &B[0] instead of B ). 当您在需要指针的上下文中使用数组时,会发生这种情况(创建了一个临时指针,它的行为与您写过&B[0]而不是B )。 That statement does not output B, it outputs the value of this temporary pointer. 该语句不输出B,而是输出该临时指针的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM