Dynamic 2D and 3D array

Static array variables are fixed in size. They are linked in data area or stack area or in const area depending on the declaration. Now these element group is fixed and cannot be srinked or enlarged. To deal with this shortcomungs dynamic array can be defined. Dynamic array is nothing but allocated during run time with malloc/calloc. Single dimention array is easy to allocate. Like take a integer pointer and call malloc/calloc with the proper memory size in bytes.

int * array = (int *) malloc(sizeof(int) * element_count);
Leter we can use this array variable in the same way as we use static array elements. Each elements can be accessed by operator[] like array[index]. Thus it is easy to convert a program written with static array to make it work with dynamic array. We need to takecare of the allocation part carefully and rest of the logic should work without any alteration.

2D Array Source Code

int main(int argc, char* argv[])
{
  int rows = 0;
  int cols = 0;
  int height = 0;
  int ***array;
  int r, c;
  printf ("2D Array has rows : ");
  scanf ("%d", &rows);
10    printf ("2D Array has columns : ");
11    scanf ("%d", &cols);
12    array = (int **) malloc (sizeof(int **)*rows);
13    for (= 0; r < rows; r++) {
14          array[r] = (int *) malloc(sizeof(int)*cols);
15 
16    }
17    for (= 0; r < rows; r++) {
18      for (= 0; c < cols; c++) {
19          printf ("Enter Array Element [%d][%d] : ", r, c);
20      scanf ("%d", &array[r][c]);
21      }
22 
23    }
24    printf ("Printing 2D Array:\n");
25    for (= 0; r < rows; r++) {
26      for (= 0; c < cols; c++) {
27          printf("%.2d ", array[r][c]);
28      }
29      printf("\n");
30 
31    }
32    return 0;
33  }

2D Array Output

2D Array has rows : 3
2D Array has columns : 4

Enter Array Element [0][0] : 1
Enter Array Element [0][1] : 2
Enter Array Element [0][2] : 3
Enter Array Element [0][3] : 4
Enter Array Element [1][0] : 5
Enter Array Element [1][1] : 6
Enter Array Element [1][2] : 7
Enter Array Element [1][3] : 8
Enter Array Element [2][0] : 9
Enter Array Element [2][1] : 10
Enter Array Element [2][2] : 11
Enter Array Element [2][3] : 12

Printing 2D Array:
01 02 03 04
05 06 07 08
09 10 11 12

3D Array Source Code

#include <stdio.h>
#include <malloc.h>
int main(int argc, char* argv[])
{
  int rows = 0;
  int cols = 0;
  int height = 0;
  int ***array;
10    int r, c, h;
11 
12    printf ("3D Array has rows : ");
13    scanf ("%d", &rows);
14    printf ("3D Array has columns : ");
15    scanf ("%d", &cols);
16    printf ("3D Array has height : ");
17    scanf ("%d", &height);
18    array = (int ***) malloc (sizeof(int ***)*height);
19    for (= 0; h < height; h++) {
20      array[h] = (int **) malloc(sizeof(int*)*rows);
21      for (= 0; r < rows; r++) {
22        array[h][r] = (int *) malloc(sizeof(int)*cols);
23      }
24    }
25    for (= 0; h < height; h++) {
26      for (= 0; r < rows; r++) {
27        for (= 0; c < cols; c++) {
28          printf ("Enter Array Element [%d][%d][%d] : ", h, r, c);
29          scanf ("%d", &array[h][r][c]);
30        }
31      }
32    }
33    printf("Printing 3D Array:\n");
34    for (= 0; h < height; h++) {
35      printf("Height %d\n", h);
36      for (= 0; r < rows; r++) {
37        for (= 0; c < cols; c++) {
38          printf("%.2d ", array[h][r][c]);
39        }
40        printf("\n");
41      }
42      printf("\n");
43    }
44    return 0;
45  }

3D Array Output

3D Array has rows : 3
3D Array has columns : 4
3D Array has height : 2
Enter Array Element [0][0][0] : 1
Enter Array Element [0][0][1] : 2
Enter Array Element [0][0][2] : 3
Enter Array Element [0][0][3] : 4
Enter Array Element [0][1][0] : 5
Enter Array Element [0][1][1] : 6
Enter Array Element [0][1][2] : 7
Enter Array Element [0][1][3] : 8
Enter Array Element [0][2][0] : 9
Enter Array Element [0][2][1] : 10
Enter Array Element [0][2][2] : 11
Enter Array Element [0][2][3] : 12
Enter Array Element [1][0][0] : 13
Enter Array Element [1][0][1] : 14
Enter Array Element [1][0][2] : 15
Enter Array Element [1][0][3] : 16
Enter Array Element [1][1][0] : 17
Enter Array Element [1][1][1] : 18
Enter Array Element [1][1][2] : 19
Enter Array Element [1][1][3] : 20
Enter Array Element [1][2][0] : 21
Enter Array Element [1][2][1] : 22
Enter Array Element [1][2][2] : 23
Enter Array Element [1][2][3] : 24

Printing 3D Array:
Height 0
01 02 03 04
05 06 07 08
09 10 11 12

Height 1
13 14 15 16
17 18 19 20
21 22 23 24

You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

token pasting, characterizing, array, multidimensional arrays, 2D and 3D dynamic array, add Matrix, multiply Matrix, adjacency Matrix,

# C Programming Language (Prentice Hall Software)
# Let Us C Paperback - 2006 by Yashavant Kanetkar
# Understanding and Using C Pointers Core techniques for memory management
# Data Structures Using C and C++ Paperback - 1998
# Data Structures In C Paperback - August 11, 2008 by Noel Kalicharan