Why can't we use double pointer to represent t

2018-12-31 09:48发布

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?

标签: c
5条回答
倾城一夜雪
2楼-- · 2018-12-31 10:10

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:

void matrix_set(double *first, size_t row_size, size_t x, size_t y, double value)
{
  first[y * row_size + x] = value;
}
查看更多
高级女魔头
3楼-- · 2018-12-31 10:15

I'm going to try to draw how

int array[10][6];

and

int **array2 = malloc(10 * sizeof *array2);
for (int i = 0; i < 10; ++i)
    array2[i] = malloc(6 * sizeof **array2);

look like in the memory and how they are different (And that they can't be cast to each other)

array looks like:

 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
| | | | | | | | | | | | | ..............| | | (10*6 elements of type int)
 - - - - - - - - - - - - - - - - - - - - - -
< first row >< second row> ...

array2 looks like:

 _ _ _ _ _ _ _ _ _ _ 
| | | | | | | | | | | (10 elements of type int *)
 - - - - - - - - - - 
 | |     ....      |     _ _ _ _ _ _
 | |                \-->| | | | | | | (6 elements of type int)
 | |                     - - - - - -
 | |
 | |      _ _ _ _ _ _
 |  \ -->| | | | | | | (6 elements of type int)
 |        - - - - - -
 |
 |
 |      _ _ _ _ _ _
  \ -->| | | | | | | (6 elements of type int)
        - - - - - -

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 for array, 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 to int ** (imagine what would happen. The int values of the cells of array are interpreted as pointers and dereferenced -> Bam! Segmentation fault!). You can however think of array as a 1d array of int [6]s; that is a 1d array of elements, with type int [6]. To write that down, you say

int (*p)[6] = array;
查看更多
宁负流年不负卿
4楼-- · 2018-12-31 10:19

In C, a two-dimensional array is an array of arrays.

You need a pointer-to-array to refer to it, not a double-pointer:

char array[2][6] = {"hello", "hai"};
char (*p)[6] = array;
//char **x = array;  // doesn't compile.

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.

查看更多
路过你的时光
5楼-- · 2018-12-31 10:23

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:

char arr[2][6] = { "hello", "hai" };
char (*p)[6] = arr;

Without any double pointer. If you want to access single characters in the above, you need to specify the element from which they come:

char* pc = *arr;

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.

查看更多
何处买醉
6楼-- · 2018-12-31 10:32

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 (w > SIZE_MAX/sizeof *m/h) goto error;
m = malloc(w * h * sizeof *m);
if (!m) goto error;
...
m[y*w+x] = foo;

If you want it to "look pretty" so you can write m[y][x], you should be using a different language, perhaps C++.

查看更多
登录 后发表回答