简体   繁体   中英

Arithmetic operation on multidimensional array

I am doing some arithmetic operations on multidimensional array. Also tried some code using 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 Array

1) 在此处输入图片说明 Again, by using dereference operator

2) 在此处输入图片说明

3D Array

3) 在此处输入图片说明 Again, by using dereference operator

4) 在此处输入图片说明

I am confused about what B and C pointing in figure 2 and 4 respectively.

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

0x7ffec3dd9f88

Here, pointer to int B is increased by ((1*4)+(1*4)) = 8 unit that is pointer to int (B+1). Though I got one explanation from Link on what actually B points in 2D array. It points first element of 2D array.

Output of my code and explanation on this Link confused me. What actually B points in 2D array? 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? Why not increased by (1*4) unit?

Code on 3D array

#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

0x7ffeac58d138

0x7ffeac58d134

C gives starting address of whole array. But, which portion specifically it points? Is it pointing &C[0] or &C[0][0] or&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. *B and B[0] are defined to mean exactly the same thing. Similarly with **B and B[0][0] .

So to fix Diagram 2, actually copy Diagram 1 but merely change the text B[0] to *B and so on. B by itself refers to the whole array. Ditto for Diagram 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.

In cout << B << endl; , treat B as saying B+0 (ie &(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 ). That statement does not output B, it outputs the value of this temporary pointer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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