I'm a lot confused with multi-dimensional arrays and pointers. I tried searching, found and read some articles. But there are still some doubts.
We can not use a double pointer(int **) for a two dimensional array, right? Instead we need to have an array of pointers with the length equal to the number of columns.

``int (*a)=b[];``

The confusion is, if we can have an array of strings(or double dimensional character array) with double char pointer(char **), why not int ** can point to double dimensional array?

## All 11 Replies

>>We can not use a double pointer(int **) for a two dimensional array, right
Wrong. Here is a brief example of how to allocate such a thing.

``````int main()
{
const int numRows = 5;
const int numColumns = 10;
double** pointer = new double*[numRows];
for(int i = 0; i < numRows; i++)
pointer[i] = new double[numColumns];
}``````

Thanks AD. We can use double pointer for two dimensional array while dynamic memory allocation.

But while passing a two dimensional array as parameter to a function accepting a double pointer argument, it doesn't works. Why?

It does work -- maybe you are just doing it wrong. Post the code that doesn't work right for you. If you want another function to allocate all that memory then you will have to pass it with three stars, not two

``````double** foo(double ***array)
{

return array;
}

int main()
{
double** array;
foo(&array);
}``````

or you can use the c++ reference operator instead of a third star

``````double** foo(double**& array)
{

return array;
}

int main()
{
double** array;
foo(array);
}``````

Thanks again. Why we need to use a triple pointer as argument while passing a double dimensional array?
Why the following code doesn't works?

``````int foo(int **a)
{
return 0;
}
int main()
{
int a;
foo(a); // ERROR
return 0;
}``````

It doesn't work because variable a is not a pointer, its an array. Try this `int foo(double [] )`

That's where I'm confused AD, a single dimension array can be passed to a single pointer argument but not a double dimension array to a double pointer argument.

I need to know the pointer arithmetic behind a double dimensional array, why is it treated as an array of pointers, rather than a double pointer as in the case of char ** which points to array of strings i.e a double dimensional character array.

The compiler must know one of the dimensions in order to calculate the location of any specific double value. If it was only given ** with no other information how could the compiler possibly calculate the location of a? In order to calculate that location you have to know the number of doubles on each row of the array, if the array is declared `a;` then the compiler calulates it something like this: a (start of the array) + 10*sizeof(double) (number of bytes to start of 2nd row) + 5*sizeof(double) (number of bytes from start of 2nd row to 5th element)

Ok, but then how it finds that when using a triple pointer? I mean how it calculates the location of a.

And in the case of char **, it can find the location because char strings have '\0' at the end?

A tripple pointer can mean one of two things:
1. A pointer to a 2d array. In this case the 3d star is not used in the calculation

2. A 3d array -- each dimension is calculated like I mentioned previously.

What about char**? How it can be used to pass double dimensional character array like in main(int argc, char **argv)?