C语言 二维数组是双指针吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7586702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:44:49  来源:igfitidea点击:

Is 2d array a double pointer?

cmultidimensional-array

提问by Angus

int main()
{
    matrix[2][4] = {{11,22,33,99},{44,55,66,110}};
    int **ptr = (int**)matrix;
    printf("%d%d",**matrix,*ptr);
}

But when a 2-d array is passed as a parameter it is typecasted into (*matrix)[2] .. what type does the compiler store this array as... is it storing as a 2-d array or a double pointer or an pointer to an array .. If it is storing as an array how does it interprets differently at different situations like above. Please help me understand.

但是当一个二维数组作为参数传递时,它被类型转换为 (*matrix)[2] .. 编译器将此数组存储为什么类型...它是存储为二维数组还是双指针或指向数组的指针.. 如果它存储为数组,它如何在不同情况下进行不同的解释,如上述。请帮我理解。

回答by jpalecek

Is 2d array a double pointer?

二维数组是双指针吗?

No. This line of your program is incorrect:

不。您程序的这一行不正确:

int **ptr = (int**)matrix;

This answer deals with the same topic

这个答案涉及相同的主题

If you want concrete image how multidimensional arrays are implemented:

如果你想要具体的图像多维数组是如何实现的:

The rules for multidimensional arrays are not different from those for ordinary arrays, just substitute the "inner" array type as element type. The array items are stored in memory directly after each other:

多维数组的规则与普通数组的规则没有区别,只是将“内部”数组类型替换为元素类型。数组项直接依次存储在内存中:

matrix: 11 22 33 99 44 55 66 110
        -----------               the first element of matrix
                    ------------  the second element of matrix

Therefore, to address element matrix[x][y], you take the base address of matrix + x*4 + y(4 is the inner array size).

因此,要解决 element matrix[x][y],您需要the base address of matrix + x*4 + y(4 是内部数组大小)。

When arrays are passed to functions, they decay to pointers to their first element. As you noticed, this would be int (*)[4]. The 4in the type would then tell the compiler the size of the inner type, which is why it works. When doing pointer arithmetic on a similar pointer, the compiler adds multiples of the element size, so for matrix_ptr[x][y], you get matrix_ptr + x*4 + y, which is exactly the same as above.

当数组传递给函数时,它们会衰减为指向其第一个元素的指针。正如您所注意到的,这将是int (*)[4]. 在4随后的类型会告诉编译器内型,这就是为什么它的大小。在对类似的指针进行指针运算时,编译器会添加元素大小的倍数,因此对于matrix_ptr[x][y],您会得到matrix_ptr + x*4 + y,这与上面的完全相同。

The cast ptr=(int**)matrixis therefore incorrect. For once, *ptrwould mean a pointer value stored at address of matrix, but there isn't any. Secondly, There isn't a pointer to matrix[1]anywhere in the memory of the program.

因此演员表ptr=(int**)matrix是不正确的。这一次,*ptr意味着一个指针值存储在矩阵的地址处,但没有。其次,没有指向matrix[1]程序内存中任何地方的指针。

Note: the calculations in this post assume sizeof(int)==1, to avoid unnecessary complexity.

注意:本文中的计算假设为sizeof(int)==1,以避免不必要的复杂性。

回答by Dmitri

No. A multidimensional array is a single block of memory. The size of the block is the product of the dimensions multiplied by the size of the type of the elements, and indexing in each pair of brackets offsets into the array by the product of the dimensions for the remaining dimensions. So..

不。多维数组是单个内存块。块的大小是维度乘以元素类型大小的乘积,每对括号中的索引通过剩余维度的维度乘积偏移到数组中。所以..

int arr[5][3][2];

is an array that holds 30 ints. arr[0][0][0]gives the first, arr[1][0][0]gives the seventh (offsets by 3 * 2). arr[0][1][0]gives the third (offsets by 2).

是一个保存 30int秒的数组。 arr[0][0][0]给出第一个,arr[1][0][0]给出第七个(偏移 3 * 2)。 arr[0][1][0]给出第三个(偏移 2)。

The pointers the array decays to will depend on the level; arrdecays to a pointer to a 3x2 int array, arr[0]decays to a pointer to a 2 element int array, and arr[0][0] decays to a pointer to int.

数组衰减到的指针将取决于级别;arr衰减为指向 3x2 int 数组arr[0]的指针,衰减为指向 2 元素 int 数组的指针,而 arr[0][0] 衰减为指向 int 的指针。

However, you can also have an array of pointers, and treat it as a multidimensional array -- but it requires some extra setup, because you have to set each pointer to its array. Additionally, you lose the information about the sizes of the arrays within the array (sizeofwould give the size of the pointer). On the other hand, you gain the ability to have differently sized sub-arrays and to change where the pointers point, which is useful if they need to be resized or rearranged. An array of pointers like this can be indexed like a multidimensional array, even though it's allocated and arranged differently and sizeofwon't always behave the same way with it. A statically allocated example of this setup would be:

然而,您也可以拥有一个指针数组,并将其视为一个多维数组——但它需要一些额外的设置,因为您必须将每个指针设置为它的数组。此外,您会丢失有关数组中数组大小的信息(sizeof将给出指针的大小)。另一方面,您可以拥有不同大小的子数组并更改指针指向的位置,这在需要调整大小或重新排列时非常有用。像这样的指针数组可以像多维数组一样索引,即使它的分配和排列方式不同,并且sizeof不会总是以相同的方式运行。此设置的静态分配示例是:

int *arr[3];
int aa[2] = { 10, 11 }, 
    ab[2] = { 12, 13 }, 
    ac[2] = { 14, 15 };
arr[0] = aa;
arr[1] = ab;
arr[2] = ac;

After the above, arr[1][0]is 12. But instead of giving the intfound at 1 * 2 * sizeof(int)bytes past the start address of the array arr, it gives the intfound at 0 * sizeof(int)bytes past the address pointed to by arr[1]. Also, sizeof(arr[0])is equivalent to sizeof(int *)instead of sizeof(int) * 2.

在以上之后,arr[1][0]12。但是,而不是给int位置找到1 * 2 * sizeof(int)过去的数组的起始地址字节arr,它给人的int位置找到0 * sizeof(int)的字节过去指向的地址arr[1]。此外,sizeof(arr[0])相当于sizeof(int *)代替sizeof(int) * 2

回答by David Schwartz

In C, there's nothing special you need to know to understand multi-dimensional arrays. They work exactly the same way as if they were never specifically mentioned. All you need to know is that you can create an array of any type, including an array.

在 C 中,理解多维数组不需要知道什么特别的东西。它们的工作方式完全相同,就好像它们从未被特别提及一样。您需要知道的是,您可以创建任何类型的数组,包括数组。

So when you see:

所以当你看到:

int matrix[2][4];

整数矩阵[2][4];

Just think, "matrixis an array of 2 things -- those things are arrays of 4 integers". All the normal rules for arrays apply. For example, matrixcan easily decay into a pointer to its first member, just like any other array, which in this case is an array of four integers. (Which can, of course, itself decay.)

试想一下,“matrix是一个由 2 个东西组成的数组——这些东西是由 4 个整数组成的数组”。适用于数组的所有常规规则。例如,matrix可以很容易地衰减为指向其第一个成员的指针,就像任何其他数组一样,在这种情况下是一个由四个整数组成的数组。(当然,它本身会衰变。)

回答by long404

If you can use the stack for that data (small volume) then you usually define the matrix:

如果您可以将堆栈用于该数据(小体积),那么您通常定义矩阵:

int matrix[X][Y]

When you want to allocate it in the heap (large volume), the you usually define a:

当你想在堆(大容量)中分配它时,你通常定义一个:

int** matrix = NULL;

and then allocate the two dimensions with malloc/calloc. You can treat the 2d array as int** but that is not a good practice since it makes the code less readable. Other then that

然后用 malloc/calloc 分配两个维度。您可以将 2d 数组视为 int** 但这不是一个好习惯,因为它使代码的可读性降低。除此之外

**matrix == matrix[0][0] is true