Pointer Arrays and Functions CPP

100 views Asked by At

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!!

2

There are 2 answers

0
anastaciu On BEST ANSWER

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.

0
Zivojin Milutinovic On

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.