Why can't we use double pointer to represent two dimensional arrays?
arr[2][5] = {"hello","hai"};
**ptr = arr;
Here why doesn't the double pointer (**ptr) work in this expample?
Why can't we use double pointer to represent two dimensional arrays?
arr[2][5] = {"hello","hai"};
**ptr = arr;
Here why doesn't the double pointer (**ptr) work in this expample?
Having pointer-to-pointer means that each row (or column, if you prefer to think of it that way) can have a different length from the other rows/columns.
You can also represent a 2D array by just a pointer to the start element, and an integer that specifies the number of elements per row/column:
I'm going to try to draw how
and
look like in the memory and how they are different (And that they can't be cast to each other)
array
looks like:array2
looks like:When you say
array[x][y]
, it translates into*((int *)array+x*6+y)
While, when you say
array2[x][y]
, it translates into*(*(array2+x)+y)
(Note that forarray
, this formula also works (read to the end of the post, and then the comments)).That is, a static 2d array is in fact a 1d array with rows put in one line. The index is calculated by the formula
row * number_of_columns_in_one_row + column
.A dynamic 2d array, however is just a 1d array of pointers. Each pointer then is dynamically allocated to point to another 1d array. In truth, that pointer could be anything. Could be
NULL
, or pointing to a single variable, or pointing to another array. And each of those pointers are set individually, so they can have different natures.If you need to pass the pointer of
array
somewhere, you can't cast it toint **
(imagine what would happen. Theint
values of the cells ofarray
are interpreted as pointers and dereferenced -> Bam! Segmentation fault!). You can however think ofarray
as a 1d array ofint [6]
s; that is a 1d array of elements, with typeint [6]
. To write that down, you sayIn C, a two-dimensional array is an array of arrays.
You need a pointer-to-array to refer to it, not a double-pointer:
For a double pointer to refer to "2-dimensional data", it must refer to the first element of an array of pointers. But a 2-dimensional array in C (array of arrays) is not the same thing as an array of pointers, and if you just define a 2-D array, then no corresponding array of pointers exists.
The only similarity between the two is the
[][]
syntax used to access the data: the data itself is structured quite differently.Let's start by talking about legal code. What you've written (assuming a char in front of each declaration) won't compile, for several reasons: you have too many initializers (six char's for arr[0], and its size is 5), and of course, char** p doesn't have a type compatible with char arr[2][5]. Correcting for those problems, we get:
Without any double pointer. If you want to access single characters in the above, you need to specify the element from which they come:
would work, if you wanted to access characters from the first element in arr.
C++ doesn't have two dimensional arrays. The first definition above defines an array[2] or array[6] of char. The implicite array to pointer conversion results in pointer to array[6] of char. After that, of course, there is no array to pointer conversion, because you no longer have an array.
Making an array of pointers to each row in order to obtain an object that "looks like" a multidimensional array of variable size is an expensive design choice for the sake of syntactic sugar. Don't do it.
The correct way to do a variable-sized multidimensional array is something like:
If you want it to "look pretty" so you can write
m[y][x]
, you should be using a different language, perhaps C++.