Why can I access an array of pointers with two parameters, when it's defined as one-dimensional?
I know, I have to work with an array of pointers to access a multi-dimensional array in a function, but I don't know why I can access the array of pointers with two parameters.
int a[m][l] { 1,2,3,4, 2,3,4,5, 3,4,5,6 }; //some matrices
int c[m][l];
int *ap[m]; //array of pointers one-dimensional
int i,j;
for (i = 0; i < m; i++) //passing the address of first element in each
ap[i] = a[i]; //colon of matrix to array of pointers
for (j = 0; j < m; j++)
bp[i] = b[i];
dosomethingwithmatrix(ap[0], bp[0], m, l);
int* dosomethingwithmatrix(const int*ap[], int* bp[])
{
cp[i][j] = ap[i][j] //accss the array of pointers with two parameters
}
In your case, ap[i][j]
is allowed because, it is meaning ful.
Let's check the data types.
For int *ap[m];
, ap
is an array of int *
s. In case of the function parameter int*ap[]
, ap
is a pointer to a pointer-to-int.
Then, ap[k]
(referring to the previous point) is an int *
. This may very well be allocated memory which can provide valid access to more that one int
s.
Provided enough memory allocated, ap[k][s]
will refer to an int
.