简体   繁体   English

我可以使用指针有一个 int 类型的二维数组吗?

[英]Can I have a 2D array of int type using pointers?

Good day guys.美好的一天,伙计们。 I am learning C and quite messed up now as I go through pointer and arrays.我正在学习 C 并且现在非常混乱,因为我通过指针和数组进行了 go。

1)Why can't I have a 2D array of int type using pointers? 1)为什么我不能使用指针有一个 int 类型的二维数组? Like喜欢

int *arr[4] = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}};

or或者

int **arr = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}};

Seems quite convincible to me though...不过对我来说似乎很有说服力...

  1. Also, why we use char * arr[5] in replace of char arr[5][40] instead of char * arr[40], that simply could not make sense to me...另外,为什么我们使用 char * arr[5] 代替 char arr[5][40] 而不是 char * arr[40],这对我来说根本没有意义......

Many thanks to your guys in advance!非常感谢你们提前!

Pointers are not arrays and arrays are not pointers.指针不是 arrays 和 arrays 不是指针。 A pointer can however be used to point at the first element in an array.然而,指针可用于指向数组中的第一个元素。

You need to make up your mind if you wish to have a 2D array of int or a 1D array of int* , each pointing the first element of an array.如果您希望拥有一个int的二维数组或一个int*的一维数组,您需要下定决心,每个数组都指向数组的第一个元素。


A 2D array is simply:二维数组很简单:

int arr[3][4] = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}};

This is the fastest form you'll ever get, but it is restricted to the scope it is allocated inside.这是您将获得的最快形式,但仅限于内部分配的 scope。


A 1D array of pointers to first elements can be done by having the pointers point at local scope arrays:指向第一个元素的一维指针数组可以通过将指针指向本地 scope arrays 来完成:

int* arr [3] = {(int[]){1,2,3,4}, (int[]){5,6,7,8}, (int[]){9,10,11,12}};

This is utilizing the feature known as compound literals .这是利用称为复合文字的功能。 These arrays will have the same scope as arr and go out of bounds when arr does.这些 arrays 将具有与arr相同的 scope 和 go 超出范围时arr


If you need the arrays to stay alive outside that scope, you could use dynamic memory allocation instead:如果您需要 arrays 在 scope 之外保持活力,您可以使用动态 memory 分配来代替:

int (*arr)[4] = malloc( sizeof(int[3][4]) );
...
arr[i][j] = something;
...
free(arr);

The above creates a dynamically allocated 2D array, then lets an array pointer point at the first array inside that 2D array.上面创建了一个动态分配的二维数组,然后让一个数组指针指向该二维数组内的第一个数组。


It's also possible to create slower, crappier code by dynamically allocating a 1D array of int* then have each pointer point at the first element of an array:也可以通过动态分配int*的一维数组然后让每个指针指向数组的第一个元素来创建更慢、更糟糕的代码:

int** arr = malloc( sizeof(int*[3]) );
for(size_t i=0; i<3; i++)
{
  arr[i] = malloc( sizeof(int[4]) );
}

...

for(size_t i=0; i<3; i++)
{
   free(arr[i]);
}
free(arr);

This amateur form has absolutely no advantage in your case.这种业余形式在您的情况下绝对没有优势。 Code like the above only makes sense when we need completely variable-sized dimensions, which we don't in case of static 2D arrays.只有当我们需要完全可变尺寸的尺寸时,上述代码才有意义,在 static 2D arrays 的情况下我们不需要。

So all this version does is to segment the heap and block data cache utilization, wasting memory and execution speed.所以这个版本所做的只是对堆和块数据缓存的利用率进行分段,浪费 memory 和执行速度。 You also need to free() it in several steps, with a loop.您还需要使用循环分几个步骤将其释放()。

More info: Correctly allocating multi-dimensional arrays更多信息: 正确分配多维 arrays

Correctly said by @Dmitri. @Dmitri 正确地说。

You can have an array of pointers, each pointing to an array... or you can have a pointer to array, pointing at an array of arrays.你可以有一个指针数组,每个指针指向一个数组……或者你可以有一个指向数组的指针,指向一个 arrays 数组。

Example:例子:

int r = 3, c = 4;

int **arr = (int **)malloc(r * sizeof(int *)); 
for (int i=0; i<r; i++) 
     arr[i] = (int *)malloc(c * sizeof(int)); 

After initialising, you can assign the values.初始化后,您可以分配值。

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

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