Conversion of 2D Array to Pointer-To-Pointer

conversion of 2D array to pointer-to-pointer

A mere conversion won't help you here. There's no compatibility of any kind between 2D array type and pointer-to-pointer type. Such conversion would make no sense.

If you really really need to do that, you have to introduce an extra intermediate "row index" array, which will bridge the gap between 2D array semantics and pointer-to-pointer semantics

Activity solution[a][b];

Activity *solution_rows[a] = { solution[0], solution[1] /* and so on */ };

Activity **mother = solution_rows;

Now accessing mother[i][j] will give you access to solution[i][j].

Converting multidimensional arrays to pointers in c++

No, there's no right way to do specifically that. A double[4][4] array is not convertible to a double ** pointer. These are two alternative, incompatible ways to implement a 2D array. Something needs to be changed: either the function's interface, or the structure of the array passed as an argument.

The simplest way to do the latter, i.e. to make your existing double[4][4] array compatible with the function, is to create temporary "index" arrays of type double *[4] pointing to the beginnings of each row in each matrix

double *startRows[4] = { startMatrix[0], startMatrix[1], startMatrix[2] , startMatrix[3] };
double *inverseRows[4] = { /* same thing here */ };

and pass these "index" arrays instead

MatrixInversion(startRows, 4, inverseRows);

Once the function finished working, you can forget about the startRows and inverseRows arrays, since the result will be placed into your original inverseMatrix array correctly.

How to convert fixed size array to pointer on pointer array

Array with two dimensions and a pointer to another pointer are not the same things. Why do you need to use mat in different ways for two different functions such as double mat[N][N] in the function getCofactor() and double **mat in the determinant() function ? Since you know what is the value of N variable, you can use it as double mat[N][N] in the determinant() function as well.

double determinant(double mat[N][N], int n)
{
double D = 0; // Initialize result

// Base case : if matrix contains single element
if (n == 1)
return mat[0][0];

double temp[N][N]; // To store cofactors

int sign = 1; // To store sign multiplier

// Iterate for each element of first row
for (int f = 0; f < n; f++)
{
// Getting Cofactor of mat[0][f]
getCofactor(mat, temp, 0, f, n); //ERORRRRRRRRRRR
D += sign * mat[0][f] * determinant(temp, n - 1);

// terms are to be added with alternate sign
sign = -sign;
}

return D;
}

Once you make the change I mentioned, your error will be fixed. Hope it helps.

Addition: I want to correct something which can cause a misunderstanding;

Array with two dimensions and a pointer to another pointer are not the
same things.

Please note that you can have something which acts like a multidimensional array with pointers. But the way how they are occupying and storing in the RAM is not same. Note that, a multidimensional array is a single block of memory. To understand it better, check the following question;

Why can't we use double pointer to represent two dimensional arrays?

Passing a 2D array as a double pointer in C

In this case, it would not be a 2D array of char, but an array of char* pointers. Like this:

void arrayInput(char **inputArray) {
printf("%s\n", inputArray[0]);
printf("%s\n", inputArray[1]);
}

int main() {
char* strings[] = { "hello", "world" };
arrayInput(strings);
}

The type of strings is of type char*[] (array of pointer to char), which decays to char** (pointer to pointer to char).

--

For a true 2D array, where the rows are concatenated in the same memory buffer, the dimension of the second row needs to be part of the type. For example

void arrayInput(int mat[][2]) {
printf("%d %d\n", mat[0][0], mat[0][1]);
printf("%d %d\n", mat[1][0], mat[1][1]);
}

int main() {
int mat[][2] = { {1, 2}, {3, 4} };
arrayInput(mat);
}

Here mat is of type int[2][2], which decays to int[][2] (array of array of 2 int) and int(*)[2] (pointer to array of 2 int).

how to assign two dimensional array to **pointer ?

The simple answer is that you cannot. A bidimensional array is a contiguous block of memory that holds each line, while a pointer to pointer can refer to a memory location where a pointer to a different memory location containing the integers is.

You can on the other hand create a separate data structure that holds the pointers to the elements in the way you want (i.e. create an array of pointers, initialize those pointers to the beginning of each row, and use a pointer to that array of pointers as pointer), but it is not useful at all, but rather will complicate everything unneedingly.

The question probably comes from the common misconceptions that arrays and pointers are the same, which they are not. An array can decay to a pointer to the first element of the array (and will do so quite often), but the type of that pointer is the type of the first element. In a bidimensional array, the type of the first element is the inner array, not the basic element type.



Related Topics



Leave a reply



Submit