Dynamic Array Creation in C

Dynamic Array Creation in C

In this article, I am going to discuss Dynamic Array Creation in C with Examples. Please read our previous articles, where we discussed Dynamic Memory Management in C.

Array Dynamic Creation in C:

Static array variables are fixed in size. They are linked in the data area or stack area or in the const area depending on the declaration. Now, this element group is fixed and cannot be srinked or enlarged. To affect this shortcoming dynamic array are often defined. Dynamic array is nothing but allocated during run time with malloc/calloc.

2D Array Dynamic Creation

There are following ways to dynamically allocate a 2D array:

Single Pointer

In this method, we simply allocate memory of size M*N dynamically and assign it to the pointer. Even though the memory is linearly allocated, we can use pointer arithmetic to index 2D array.

Program: 2D Array Dynamic Creation using Single Pointer in 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));

    return 0;
}

Output: 2D Array Dynamic Creation using Single Pointer in C

Pointer to a Pointer (Double Pointer)

To develop 2D array dynamically, we are required to take a pointer to pointer variable then one array is required to create and to manage multiple rows.

Program: 2D Array Dynamic Creation using Pointer to Pointer in 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));

    count = 0;
    for (i = 0; i < r; i++)
        for (j = 0; j < c; j++)
            arr[i][j] = ++count;

    for (i = 0; i < r; i++)
        for (j = 0; j < c; j++)
            printf ("%d ", arr[i][j]);

    return 0;
}

Output: 2D Array Dynamic Creation using Pointer to Pointer in C

3D Array Dynamic Creation

There are following ways to dynamically allocate a 3D array:

Single Pointer

In this approach, we simply allocate memory of size M*N*O dynamically and assign it to a pointer. Even though the memory is linearly allocated, we can use pointer arithmetic to index 3D array.

Program: 3D Array Dynamic Creation using Single Pointer in C
#include<stdio.h>
#include<stdlib.h>
#define M 2
#define N 3
#define O 4
int main ()
{
    int i, j, k;
    int *A = (int *) malloc (M * N * O * sizeof (int));
    if (A == NULL)
    {
        fprintf (stderr, "Out of Memory");
        exit (0);
    }
    for (i = 0; i < M; i++)
        for (j = 0; j < N; j++)
            for (k = 0; k < O; k++)
             *(A + i * N * O + j * O + k) = rand () % 100;

    for (i = 0; i < M; i++)
    {
        for (j = 0; j < N; j++)
     {
         for (k = 0; k < O; k++)
             printf ("%d", *(A + i * N * O + k));
         printf ("\n");
     }
        printf ("\n");
    }
    free (A);
    return 0;
}

Output:

3D Array Dynamic Creation using Single Pointer in C

Triple Pointer:

Implementing multi-dimensional arrays in C is very similar, we use malloc()\free()  stdlib methods instead of the new\delete keywords. 

Program: 3D Array Dynamic Creation using Triple Pointer in C
#include <stdio.h>
#include <stdlib.h>
void main ()
{
    int x = 3, y = 4, z = 5;
    int i, j, k;
    int *allElements = malloc (x * y * z * sizeof (int));
    int ***array3D = malloc (x * sizeof (int **));

    for (i = 0; i < x; i++)
    {
        array3D[i] = malloc (y * sizeof (int *));
        for (j = 0; j < y; j++)
     {
         array3D[i][j] = allElements + (i * y * z) + (j * z);
     }
    }

    for (i = 0; i < x; i++)
    {
        printf ("%d\n", i);
        for (j = 0; j < y; j++)
     {
         printf ("\n");

         for (k = 0; k < z; k++)
         {
             array3D[i][j][k] = (i * y * z) + (j * z) + k;
             printf ("\t%d", array3D[i][j][k]);
         }
     }
        printf ("\n\n");
    }
    
    free (allElements);
    for (i = 0; i < x; i++)
    {
        free (array3D[i]);
    }
    free (array3D);
}

Output:

3D Array Dynamic Creation using Triple Pointer in C

In the next article, I am going to discuss File Handling in C with Examples. Here, in this article, I try to explain Dynamic Array Creation in C. I hope you enjoy this article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *