1

I would like to understand how does it works to scan a dynamically allocated two-dimensional array using pointers arithmetics?

I can't figurate why in my example pointer arithmetic isn't returning the same results as using array indexing.

Here is my implementation:

    #include <iostream>
    using namespace std;

void init_matrix(int ** m, int size);
void print_matrix(int ** m, int size);
void print_matrix_pointers(int ** m, int size);

int main (){
    srand (time(NULL));
    int size = 3;
    int ** dynamic_matrix = new int * [size];
    for (int i = 0; i < 10; i++) {
        dynamic_matrix[i] = new int [size];
    }
    init_matrix(dynamic_matrix, size);
    
    cout << "Dynamic matrix accessed using square brackets ([i][j]): " << endl;
    print_matrix(dynamic_matrix, size);

    cout << "Dynamic matrix accessed using pointers arithmetics: " << endl;
    print_matrix_pointers(dynamic_matrix, size);

    return 0;

}

void init_matrix(int ** m, int size) {
    for (int i = 0; i < size; i++){
        for (int j = 0; j < size; j++){
            m[i][j] = rand()%10;
        }
    }
}

void print_matrix(int ** m, int size){
    for (int i = 0; i < size; i++){
        for (int j = 0; j < size; j++){
            cout << m[i][j] << " ";
        }
        cout << endl;
    }
}


void print_matrix_pointers(int ** m, int size){
    for (int i = 0; i < size; i++){
        for (int j = 0; j < size; j++){
            cout << *(*m + (i * size) + j) << " "; //
        }
        cout << endl;
    }
    cout << endl;
}

For instance if size was 3 I would get this output.

Dynamic matrix accessed using array indexing ([i][j]):

3 3 4

9 5 9

4 9 4

Dynamic matrix accessed using pointers arithmetics:

3 3 4

32735 9 5

9 32735 4

6
  • int size = 3; but for (int i = 0; i < 10; i++) { thats undefined behavior. Output of the code could be anything. Commented Feb 11, 2022 at 10:54
  • Thanks i forgot to change that. Commented Feb 11, 2022 at 10:58
  • please dont fix errors in the quesiton. The question is for the broken code. Corrected code goes to answers Commented Feb 11, 2022 at 10:58
  • 1
    If you use std::vector and appropriate functions and statements (like range for loops), you can avoid many problems. Commented Feb 11, 2022 at 10:59
  • Could you please clarify the question to explain that you are trying to understand why your pointer arithmetic example is not returning the same results as your array indexing? Commented Feb 12, 2022 at 2:46

2 Answers 2

2

With *(*m + (i * size) + j) you treat m as a contiguous array of values, which it isn't. It's more like a jagged array. You need to treat it like the array of pointers it really is, like *(*(m + i) + j).

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

Comments

1

Two typos in your code:

int size = 3;
int ** dynamic_matrix = new int * [size];
for (int i = 0; i < 10; i++) {
    dynamic_matrix[i] = new int [size];
}

size is 3 but you write 10 elements. That is undefined behavior.

Then you are advancing the pointer wrongly.

void print_matrix_pointers(int ** m, int size){
    for (int i = 0; i < size; i++){
        for (int j = 0; j < size; j++){
            cout << *( *(m + i) + j) << " "; //
        }
        cout << endl;
    }
    cout << endl;
}

m points to an array of pointers to int. You have to increment m by i to get to the i-th row. Then you want to access the jth element of the array pointed to by m[i], hence you have to first dereference m+i then add j to get the address of jth element.

Comments

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.