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.

Dynamic allocation of 1d array in c

Single dimention array is easy to allocate. Like take a integer pointer and call malloc/calloc with the proper memory size in bytes.

/* C allocation */
int * array = (int *) malloc(sizeof(int) * element_count);

/* C++ allocation */
int * array = (int *) new 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.

/* Code using Dynamic allocation of 1d array */
int i;
int * array = (int *) malloc(sizeof(int) * element_count);
for (= 0; i < element_count; i++)
{
   /*use array[i] here */
}

/* Code using static allocation of 1d array */
int i;
const int element_count = 8;
int array[element_count];
for (= 0; i < element_count; i++)
{
   /*use array[i] here */
}

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);
  printf ("2D Array has columns : ");
  scanf ("%d", &cols);
  array = (int **) malloc (sizeof(int **)*rows);
  for (= 0; r < rows; r++) {
        array[r] = (int *) malloc(sizeof(int)*cols);

  }
  for (= 0; r < rows; r++) {
    for (= 0; c < cols; c++) {
        printf ("Enter Array Element [%d][%d] : ", r, c);
    scanf ("%d", &array[r][c]);
    }

  }
  printf ("Printing 2D Array:\n");
  for (= 0; r < rows; r++) {
    for (= 0; c < cols; c++) {
        printf("%.2d ", array[r][c]);
    }
    printf("\n");

  }
  return 0;
}

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;
  int r, c, h;

  printf ("3D Array has rows : ");
  scanf ("%d", &rows);
  printf ("3D Array has columns : ");
  scanf ("%d", &cols);
  printf ("3D Array has height : ");
  scanf ("%d", &height);
  array = (int ***) malloc (sizeof(int ***)*height);
  for (= 0; h < height; h++) {
    array[h] = (int **) malloc(sizeof(int*)*rows);
    for (= 0; r < rows; r++) {
      array[h][r] = (int *) malloc(sizeof(int)*cols);
    }
  }
  for (= 0; h < height; h++) {
    for (= 0; r < rows; r++) {
      for (= 0; c < cols; c++) {
        printf ("Enter Array Element [%d][%d][%d] : ", h, r, c);
        scanf ("%d", &array[h][r][c]);
      }
    }
  }
  printf("Printing 3D Array:\n");
  for (= 0; h < height; h++) {
    printf("Height %d\n", h);
    for (= 0; r < rows; r++) {
      for (= 0; c < cols; c++) {
        printf("%.2d ", array[h][r][c]);
      }
      printf("\n");
    }
    printf("\n");
  }
  return 0;
}

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

About our authors: Team EQA

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

#