以下是在堆上创建2D数组(或动态分配2D数组)的不同方法。 在下面的例子中,我们考虑了 R “作为行数,” C ‘作为列数,我们创建了一个二维数组,其中r=3,c=4和以下值
null
1 2 3 4 5 6 7 8 9 10 11 12
1) 使用单指针和带指针算法的一维数组: 一种简单的方法是分配大小为r*c的内存块,并使用简单的指针算法访问其元素。
C
#include <stdio.h> #include <stdlib.h> int main( void ) { int r = 3, c = 4; int * ptr = malloc ((r * c) * sizeof ( int )); /* Putting 1 to 12 in the 1D array in a sequence */ for ( int i = 0; i < r * c; i++) ptr[i] = i + 1; /* Accessing the array values as if it was a 2D array */ for ( int i = 0; i < r; i++) { for ( int j = 0; j < c; j++) printf ( "%d " , ptr[i * c + j]); printf ( "" ); } free (ptr); return 0; } |
输出:
1 2 3 45 6 7 89 10 11 12
2) 使用指针数组 我们可以创建一个大小为r的指针数组。注意,从C99开始,C语言允许可变大小的数组。创建指针数组后,我们可以为每一行动态分配内存。
C
#include <stdio.h> #include <stdlib.h> int main() { int r = 3, c = 4, i, j, count; int * arr[r]; for (i = 0; i < r; i++) arr[i] = ( int *) malloc (c * sizeof ( int )); // Note that arr[i][j] is same as *(*(arr+i)+j) count = 0; for (i = 0; i < r; i++) for (j = 0; j < c; j++) arr[i][j] = ++count; // Or *(*(arr+i)+j) = ++count for (i = 0; i < r; i++) for (j = 0; j < c; j++) printf ( "%d " , arr[i][j]); /* Code for further processing and free the dynamically allocated memory */ for ( int i = 0; i < r; i++) free (arr[i]); return 0; } |
输出:
1 2 3 4 5 6 7 8 9 10 11 12
3) 使用指针指向指针 我们也可以使用双指针动态创建指针数组。一旦我们有了一个动态分配的数组指针,我们就可以像方法2一样为每一行动态分配内存和内存。
C
#include <stdio.h> #include <stdlib.h> int main() { int r = 3, c = 4, i, j, count; int ** arr = ( int **) malloc (r * sizeof ( int *)); for (i = 0; i < r; i++) arr[i] = ( int *) malloc (c * sizeof ( int )); // Note that arr[i][j] is same as *(*(arr+i)+j) count = 0; for (i = 0; i < r; i++) for (j = 0; j < c; j++) arr[i][j] = ++count; // OR *(*(arr+i)+j) = ++count for (i = 0; i < r; i++) for (j = 0; j < c; j++) printf ( "%d " , arr[i][j]); /* Code for further processing and free the dynamically allocated memory */ for ( int i = 0; i < r; i++) free (arr[i]); free (arr); return 0; } |
输出:
1 2 3 4 5 6 7 8 9 10 11 12
4) 使用双指针和一个malloc调用
C
#include<stdio.h> #include<stdlib.h> int main() { int r=3, c=4, len=0; int *ptr, **arr; int count = 0,i,j; len = sizeof ( int *) * r + sizeof ( int ) * c * r; arr = ( int **) malloc (len); // ptr is now pointing to the first element in of 2D array ptr = ( int *)(arr + r); // for loop to point rows pointer to appropriate location in 2D array for (i = 0; i < r; i++) arr[i] = (ptr + c * i); for (i = 0; i < r; i++) for (j = 0; j < c; j++) arr[i][j] = ++count; // OR *(*(arr+i)+j) = ++count for (i = 0; i < r; i++) for (j = 0; j < c; j++) printf ( "%d " , arr[i][j]); return 0; } |
输出:
1 2 3 4 5 6 7 8 9 10 11 12
幸亏 特里桑什·巴尔德瓦伊 感谢你提出第四种方法。 本文由 阿比拉蒂 。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请发表评论
5) 使用指向可变长度数组的指针。
VLA的维度与变量的类型有关。因此,其中一个会形成一个指向具有运行时定义形状的数组的指针。 在使用语法(*arr)[i][j]订阅之前,必须取消对指针的引用。
C
#include <stdio.h> #include <stdlib.h> int main() { int row = 3, col = 4, i, j, count; int (*arr)[row][col] = malloc ( sizeof *arr); count = 0; for (i = 0; i < row; i++) for (j = 0; j < col; j++) (*arr)[i][j] = ++count; for (i = 0; i < row; i++) for (j = 0; j < col; j++) printf ( "%d " , (*arr)[i][j]); free (arr); return 0; } |
6) 使用指向VLA第一行的指针
与5类似,但允许使用arr[i][j]语法。
C
#include <stdio.h> #include <stdlib.h> int main() { int row = 3, col = 4, i, j, count; int (*arr)[col] = calloc (row, sizeof *arr); count = 0; for (i = 0; i < row; i++) for (j = 0; j < col; j++) arr[i][j] = ++count; for (i = 0; i < row; i++) for (j = 0; j < col; j++) printf ( "%d " , arr[i][j]); free (arr); return 0; } |
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END