以下是在堆上创立2D数组(或动态分配2D数组)的不同办法。

在以下示例中, 咱们思考了"[R‘作为行数, ‘C‘作为列数, 咱们创立了一个2D数组, 其中r = 3, c = 4和以下值

1  2  3  4  5  6  7  8  9  10 11 12

1)应用单个指针:

一种简略的办法是应用简略的指针算法调配大小为r * c的存储块和拜访元素。

#include <stdio.h>#include <stdlib.h>  int main(){     int r = 3, c = 4;     int *arr = ( int *) malloc (r * c * sizeof ( int ));       int i, j, count = 0;     for (i = 0; i <  r; i++)       for (j = 0; j < c; j++)          *(arr + i*c + j) = ++count;       for (i = 0; i <  r; i++)       for (j = 0; j < c; j++)          printf ( "%d " , *(arr + i*c + j));      /* Code for further processing and free the        dynamically allocated memory */        return 0;}

输入如下:

1 2 3 4 5 6 7 8 9 10 11 12

2)应用指针数组

咱们能够创立大小为r的指针数组。请留神, 从C99开始, 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 */      return 0;}

输入如下:

1 2 3 4 5 6 7 8 9 10 11 12

3)应用指向指针的指针

咱们还能够应用双指针动态创建指针数组。一旦咱们动态分配了数组指针, 就能够像办法2一样为每行动静分配内存。

#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 */      return 0;}

输入如下:

1 2 3 4 5 6 7 8 9 10 11 12

4)应用双指针和一个malloc调用

#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

如果发现任何不正确的中央, 或者想分享无关上述主题的更多信息, 请发表评论。

更多C语言相干内容请参考:lsbin - IT开发技术:https://www.lsbin.com/

查看以下更多C语言的内容:

  • C语言exec函数家族:https://www.lsbin.com/3438.html
  • C语言extern关键字艰深解释:https://www.lsbin.com/2950.html
  • C语言中的scanset是什么?:https://www.lsbin.com/2363.html