0

I wonder If there's a way to assign a dynamic array (C style), values like in the case of a non dynamic array (instead of matrix[0][0] = ...), e.g.:

int n = 3;
int ** matrix = new int*[n];

for(int i = 0; i < n; ++i){
     matrix[i] = new int[n];
}

matrix =  {{1,1,1},{2,0,2},{3,3,3}};

And how would I pass the non dynamic array int matrix[3][3] = {{1,1,1},{2,0,2},{3,3,3}}; to a function like void printmatrix(int **matrix, int n)?

Thanks!!

5
  • Why not use std::vector and std::array? Commented Oct 22, 2020 at 19:03
  • @MooingDuck dunno, are there disadvantages in O time or space? Commented Oct 22, 2020 at 19:08
  • not if you use them correctly, no Commented Oct 22, 2020 at 19:09
  • 1
    std::array is a zero-cost abstraction over a C array. Commented Oct 22, 2020 at 19:10
  • 1
    Here is a simple vector based, high speed matrix implementation that is nearly foolproof.. Commented Oct 22, 2020 at 19:15

2 Answers 2

4

I wonder If there's a way to assign a dynamic array (in c), values like in the case of a non dynamic array (instead of matrix[0][0] = ...)

For 2D arrays, only on declaration, e.g.:

int matrix[][3] = {{1,1,1},{2,0,2},{3,3,3}};

Or

int matrix[][3]{{1, 1, 1}, {2, 0, 2}, {3, 3, 3}};

Value initialization of arrays is allowed. After that you can't, matrix is not a modifiable lvalue, you can't directly assing values in such fashion.

Later adding values is exactly the same in both situations, using matrix[0][0] = ..., this is valid for both 2D arrays and for pointer to pointer allocations, it's a fine substitute for pointer dereference notation I might add.

And how would I pass the non dynamic array int matrix[3][3] = {{1,1,1},{2,0,2},{3,3,3}}; to a function like void printmatrix(int **matrix, int n)?

It can't be done, one is a pointer to pointer to int, the other is an array of arrays of ints, aka a 2D array of int's, these are incompatible, you cannot pass any one of them to an argument of the other.

At most you can pass an array of pointers, i.e. int* matrix[SIZE] can be passed to an argument of type int **matrix, and this is because the array will decay to a pointer when passed as an argument.

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

Comments

1

As had previously been mentioned,this kind of assignment is not possible in C++.Each element of a matrix is just a pointer to an element of type int.

enter image description here

This image shows what are you trying to do when you dynamically allocate a matrix.Dynamic allocation is used when you have to give values to matrix fields at runtime,for example you get data from the user with std::cin and then you populate a matrix with that data dynamically.

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.