0

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)enter image description here Again, by using dereference operator

2)enter image description here

3D Array

3)enter image description here Again, by using dereference operator

4)enter image description here

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.

1 Answer 1

1

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 (i.e. &(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.

Sign up to request clarification or add additional context in comments.

4 Comments

Then, C[0] == *C, C[0][0] == **C and C[0][0][0] == ***C. Am I correct?
@zipperblock C[0] has exactly the same meaning as *C , etc. I avoid saying == because that is an operator which performs conversions on its arguments. As an expression, the last one might not even be possible if you used a struct type instead of int for example.
If I want output from *(B +1), it gives base address of 2nd 1D array address in 2D array. But, B is &B[0] as you said then adding one it will give &B[1] and by dereferencing that is *(&B[1]) it is giving &B[1][0]. Am i correct?
@zipperblock *&X means X. So *&B[1] is B[1].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.