Trying to print adresses of 2D dynamic arrays

I've created a program that is able to create a staggered 2D dynamic array.
I pass this array to function and try to cout it's "childrens" memory locations in two ways:

1
2
cout << arr[i] << " : ";
cout << &arr[i] << " : ";


Both of these return different memory locations.
For example:

0x556ec382dbf0 : 0x556ec382d2c8 : 
0x556ec382dc30 : 0x556ec382d2d0 :
0x556ec382dc50 : 0x556ec382d2d8 :


I know that & is address operator and it should return the address of the variable, but I still don't understand the difference between what these two print.

Here's the full code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
int main(){
    int size, temp, temp2;

    cin >> size;

    int **arr = new int*[size];
    int *sizes = new int[size];

    for(int i = 0; i < size; i++){
        cin >> temp;    

        arr[i] = new int[temp];
        sizes[i] = temp;
    }
    
    while(true){
        print2DSArray(arr, sizes, size);

        cout << "Which row change? : ";
        cin >> temp;
        cout << "To what lenght? : ";
        cin >> temp2;

        change2DSArrayRowL(arr, sizes, size, temp, temp2);
    }
}


void print2DSArray(int **arr, int *sizes, int size){
    for(int i = 0; i < size; i++){
        cout << arr[i] << " : ";
        cout << &arr[i] << " : ";
        for(int j = 0; j < sizes[i]; j++){
            cout << arr[i][j] << " ";
        }
        cout << endl;
    }
}
Last edited on
arr[i] is the value stored in the i'th element of arr. That means it is the address of the memory allocated at line 12.

&arr[i] is the memory address of the i'th element of arr. That means it will be one of the addresses in the range of memory allocated at line 6.
Topic archived. No new replies allowed.