Variable Length Array in C Programming


In this article, you will learn how to create Variable Length Array in C Programming (VLAs) such as one-dimensional and multi-dimensional arrays.

variable length array in c

Until now, we have used an array that had constant size. The size of the array is fixed.

Wait for a second, what if you don’t know array size at compilation time?

Here comes the importance of variable length array in C programming whose length or size is evaluated at execution time. We can easily declare one dimensional, two dimensional and multi-dimensional arrays.

Note: sizeof operator when used in variable length array operates at run time instead of at compile time.

Example of Variable length array in C

void oneDArray( int n, int arr[ n ];
void twoDArray( int row, int col, int arr[ row ][ col ];

In the above example, we see that function parameters of oneDArray and twoDArray are declared with variable length array type.

The size of variable length array in c programming must be of integer type and it cannot have an initializer.

We know that two array types are compatible if:

  • Both arrays must have compatible element types.
  • If both size specifiers are present and are integer type, then both must have the same value.

However, two variable length arrays are always compatible if they both have the same element type.

Demonstration of variable length array in C

#include <stdio.h>

void oneDArray( int length, int a[ length ]); //function prototype
void twoDArray( int row, int col, int a[ row ][ col ]); //function prototype

int main()
{
   int i, j; //counter variable
   int size; //variable to hold size of one dimensional array
   int row1, col1, row2, col2; //number of rows & columns of two D array

   printf("Enter size of one-dimensional array: ");
   scanf("%d", &size);

   printf("Enter number of rows & columns of 2-D array:\n");
   scanf("%d %d", &row1, &col1);

   printf("Enter number of rows & columns of multi-D array:\n");
   scanf("%d %d", &row2, &col2);

   //declaring arrays
   int arr[ size ];
   int arr2D[ row1 ][ col1 ]; //2-D array
   int arrMD[ row2 ][ col2 ]; //multi-dimensional array

   //one dimensional array
   for ( i = 0; i < size; ++i)
   {
      arr[ i ] = 2 * i;
   }

   //two dimensional array
   for ( i = 0; i < row1; ++i)
   {
      for ( j = 0; j < col1; ++j)
      {
      arr2D[ i ][ j ] = i + j;
      }
   }

   //multi-dimensional array
   for ( i = 0; i < row2; ++i)
   {
      for ( j = 0; j < col2; ++j)
      {
         arrMD[ i ][ j ] = i + j;
      }
   }

   //printing arrays
   printf("One-dimensional array:\n");
   oneDArray( size, arr );

   printf("Two-dimensional array:\n");
   twoDArray( row1, col1, arr2D );

   printf("\nMulti-dimensional array:\n");
   twoDArray( row2, col2, arrMD );

   return 0;
}  //end main

void oneDArray( int length, int a[ length ])
{
   int i;
   for ( i = 0; i < length; ++i)
   printf("a[%d] : %d\n", i, a[ i ]);
 }  //end of oneDArray

void twoDArray( int row, int col, int a[ row ][ col ])
{
   int i, j;
   for ( i = 0; i < row; ++i )
   {
      printf("\n");
      for ( j = 0; j < col; ++j )
         printf("%5d", a[i][j]);
   }
}  //end twoDArray

Output
variable length array in C

Explanation

In the above program, we have declared and printed three variable length array. First, we asked the user to enter the size for a one-dimensional array, two-dimensional and, multi-dimensional array.

There are two user-defined functions oneDArray () and twoDArray () for printing arrays.

Function oneDArray () takes size as parameter whereas, twoDArray takes row and col as its parameter.

First, all the elements in the first row of the multi-dimensional array are filled, then proceed for the second row and so on.