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.
Pointers with multi dimensional arrays in C
Educate yourself on multidimensional arrays.
Array array
is a 2D array:
int array[2][3] = {{2,3,6},{4,5,8}};
*array
is the first element of array array
because
*array -> * (array + 0) -> array[0]
The first element of array array
is array[0]
, which is {2,3,6}
. The type of array[0]
is int [3]
.
When you access an array, it is converted to a pointer to first element (there are few exceptions to this rule).
So, in this statement
printf("%d\n",*array);
*array
will be converted to type int *
. The format specifier %d
expect the argument of type int
but you are passing argument of type int *
. The compiler must be throwing warning message for this. Moreover, wrong format specifier lead to undefined behaviour.
If you want to print a pointer, use %p
format specifier. Remember, format specifier %p
expect that the argument shall be a pointer to void
, so you should type cast pointer argument to void *
.
Pointer variable pointing to a one dimensional array or two dimensional array?
If you have an object of some type T
like
T a;
then declaration of a pointer to the object will look like
T *p = &a;
Your array b
has the type int[4]
. So a pointer to the array will look like
int ( *p )[4] = &b;
To output the second element of the array using the pointer you should write
printf("%d \n", *( *p + 1 ) );
Thus your compiler issued the error message
cannot convert from 'int [4]' to 'int (*)[4]
because instead of writing at least
int ( *p )[4] = &b;
you wrote
int ( *p )[4] = b;
On the other hand, an array designator used in expressions with rare exceptions is implicitly converted to pointer to its first element. For example in this declaration
int *p = b;
the array b
used as an initializer is converted to pointer to its firs element. The above declaration is equivalent to
int *p = &b[0];
or that is the same
int *p = b + 0;
Using this pointer you can call the function printf
like
printf("%d \n", *(p + 1));
If you have a two-dimensional array as
int a[3][4];
then used in expressions it is converted to pointer to its first element that has the type int[4]
. So you may write
int ( *p )[4] = a;
If you want to declare a pointer to the whole array as a single object you can write
int ( *p )[3][4] = &a;
convert array to two dimensional array by pointer
You can't initialise an int bla[2][3]
with an int*
(what foo
becomes in that context).
You can achieve that effect by declaring a pointer to arrays of int
,
int (*bla)[3] = (int (*)[3])&foo[0];
but be sure that the dimensions actually match, or havoc will ensue.
Related Topics
C++ Fast Screenshots in Linux for Use with Opencv
Illegal Token on Right Side of ::
Boost Asio Single Threaded Performance
Openprocess/Readprocessmemory/Writeprocessmemory/Closehandle Equivalent
Press Anykey to Continue in Linux C++
C++ Portability Between Windows and Linux
Way Cross Compile C/C++ Code to Run on Windows, Linux, and MAC Os
Cannot Get Makefile to Build Each Object from Its Corresponding Source
Missing Lboost_Thread-Mt in Mongodb Cpp Driver (Ubuntu Server X64)