简体   繁体   English

C中的多维数组

[英]Multi-dimensional arrays in C

I was kind of a Java-holic. 我有点像Java-holic。 I wanted to learn more about arrays in C. Is there such an array: 我想了解更多关于C中的数组。是否有这样的数组:

int test[5][5][5]

I want to know how many integer elements it has. 我想知道它有多少个整数元素。 I had three questions: 我有三个问题:

  1. I think there are 125 int type elements. 我认为有125个int类型元素。 Is that correct? 那是对的吗?
  2. Is it correct that test has 125 *int type elements? test有125 *int类型元素是否正确?
  3. Is it correct that test has 5 **int type elements? test有5 **int类型元素是否正确?

firstly, i think there are 125 int type elements. 首先,我认为有125个int类型元素。

That's right. 那就对了。 It's an array of 5 arrays of 5 arrays of 5 int s, which means there's a total of 125 int s. 它是由5个int组成的5个数组的5个数组的数组,这意味着总共有125个int

secondly, is it correct that above 'test' array has 125 *int type elements? 其次,上面'test'数组有125 * int类型元素是否正确?

*int is not a valid type - presumably you mean int* . *int不是有效类型 - 大概是指int* Either way, the answer is no. 无论哪种方式,答案都是否定的。 There are no pointers here. 这里没有指针。

thirdly, is it correct that the 'test' array has 5 **int type elements? 第三,'test'数组有5 ** int类型元素是否正确?

Again, **int is probably meant to be int** , but the answer is no. 同样, **int可能意味着是int** ,但答案是否定的。 No pointers in this declaration. 本声明中没有指针。

Presumably you're asking this because you have some idea that "arrays are pointers" or "arrays are just pointers under the hood" - this idea is wrong. 大概是你问这个,因为你有一些想法,“数组是指针”或“数组只是指针下的指针” - 这个想法是错误的。 When you ask for an int[5][5][5] , you get exactly that - a 3-dimensional array consisting of a total of 125 int objects. 当你要求int[5][5][5] ,你就得到了 - 一个由总共125个int对象组成的三维数组。

这个数组有125个int类型的元素,除了这些整数之外它不包含任何东西。

Let consider declaration 让我们考虑宣言

int test[5][5][5];

step by step. 一步步。

At first consider declaration like 首先考虑声明如

int test[5];

test has 5 elements of type int . test有5个int类型的元素。

Now consider 现在考虑

int test[5][5];

It declares an array of 5 elements of the preceding type int[5] . 它声明了一个包含前面类型int[5]的5个元素的数组。 So as each element of the array in turn an array and contains 5 elements then the total number of elements of type int contained in test is equal to 5 * 5 that is 25. 因此,当数组的每个元素依次为一个数组并包含5个元素时,test中包含的int类型的元素总数等于5 * 5即25。

Now let's return to declaration 现在让我们回到宣言

int test[5][5][5];

It defines an array elements of which has type int[5][5] . 它定义了一个数组元素,其类型为int[5][5] Each element contains 25 objects of type int . 每个元素包含25个int类型的对象。 The array has 5 such elements. 该阵列有5个这样的元素。 So in total it contains 5 * 25 objects of type int that is 125 such objects. 所以它总共包含5 * 25个int类型的对象,这是125个这样的对象。

So array 所以阵列

int test[5][5][5];

has 5 elements of type int[5][5] 有5个int[5][5]类型的元素int[5][5]

array 排列

int test[5][5];

has 5 elements of type int[5] 有5个int[5]类型的元素int[5]

and array 和数组

int test[5];

has 5 elements of type int . 有5个int类型的元素。

In C++ a multidimensional array is an array elements of which are in turn arrays (not pointers). 在C ++中,多维数组是一个数组元素,它们又是数组(而不是指针)。

Only the first statement is correct. 只有第一个陈述是正确的。 And you also have another guarantee: multi-dimensional arrays in C++ (notice that C and C++ are two different languages) allocate contiguous memory blocks 而且你还有另一个保证:C ++中的多维数组(注意C和C ++是两种不同的语言)分配连续的内存块

[dcl.array] An object of array type contains a contiguously allocated non-empty set of N subobjects of type T. [dcl.array]数组类型的对象包含一个类型为T的连续分配的非空N个子对象集。

so the following holds: 以下是:

int array[5][5][5];
int array2[125];

std::cout << (sizeof(array) == sizeof(array2)); // Yes

Indices are an abstraction over a different access-pattern. 指数是对不同访问模式的抽象。

The second is not correct ( int* is a different type than int ). 第二个不正确( int*是与int不同的类型)。 And even in memory-space terms it is generally not guaranteed that an array of 125 integer elements will occupy the same space as an array of 125 integer pointers. 即使在内存空间方面,通常也不能保证125个整数元素的数组将占用与125个整数指针数组相同的空间。

Ditto for the third one. 同上第三个。

As a sidenote: arrays are often manipulated through pointers and there's an entire topic called " pointers arithmetic " which goes hand-in-hand with arrays, not to mention arrays decaying into pointers but arrays are not pointers . 作为旁注:数组通常通过指针进行操作,并且有一个称为“ 指针算术 ”的完整主题,它与数组密切相关,更不用说数组衰减成指针数组不是指针

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM