• C Programming Video Tutorials

Variable Length Arrays in C



A Variable length array in C are also called variable sized or runtime−sized arrays. It is an array whose length is determined at the run−time rather than at the time of compiling the program. Is size depends on a value generated during the run−time of a program, usually received as the input from the user.

Usually, the array size is declared beforehand in the program as follows −

int arr[10];

The size of an array once declared is fixed during the execution of the program and cannot be changed during its run−time. However, support to VLA (Variable Length Array) was added in the C99 standard. In case of a VLA, the compiler allocates the memory with automatic storage duration on the stack.

The syntax of creating variable length arrays is as follows −

void arr_init(int length){
   int arr[length];
   //code using arr;
};

The variable length array is fast and more straightforward option compared to heap−allocation. VLAs are supported by most modern C compilers such as GCC, Clang etc. and are used by most compilers.

VLAs can also be allocated on the heap and internally accessed using a pointer to this block.

Example

#include <stdio.h>
int main(){
   int i, j;
   int size; //variable to hold size of one dimensional array

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

   int arr[ size ];
   for ( i = 0; i < size; ++i){
      printf("Enter a number: ");
      scanf("%d", &j);
      arr[ i ] = j;
   }

   for ( i = 0; i < size; ++i)
   printf("a[%d] : %d\n", i, arr[ i ]);

   return 0;
}

Output

Enter size of one-dimensional array: 5
Enter a number: 1
Enter a number: 5
Enter a number: 7
Enter a number: 8
Enter a number: 7
a[0] : 1
a[1] : 5
a[2] : 7
a[3] : 8
a[4] : 7

Similarly, we can declare and use a 2−dimensional VLA.

#include <stdio.h>
int main(){
   int i, j, x;
   int row, col; //number of rows & columns of two D array

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

   int arr2D[ row ][ col ];
   for ( i = 0; i < row; ++i){
      for ( j = 0; j < col; ++j){
         printf("Enter a number: ");
         scanf("%d", &x);
         arr2D[ i ][ j ] = x;
      }
   }

   for ( i = 0; i < row; ++i ){
      printf("\n");
      for ( j = 0; j < col; ++j )
         printf("%5d", arr2D[i][j]);
   }

   return 0;
}

The following code declares a variable length one dimensional array populates them with incrementing numbers.

Example

#include <stdio.h>

int main(){
   int n;
    
   printf("Enter the size of the array: \n");
   scanf("%d", &n);
    
   int arr[n];
    
   for(int i=0; i<n; i++)
      arr[i] = i+1;
    
   printf("The array elements are: ");

   for(int i=0; i<n; i++)
      printf("%d ", arr[i]);
    
   return 0;
}

Output

Enter the size of the array: 
5
The array elements are: 1 2 3 4 5 ....

Following code fills the variable length array with randomly generated numbers with srand() and rand() functions from stdlib.h header file.

Example

#include <stdio.h>
#include <stdlib.h>
#include <time.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 row, col; 
   
   //number of rows & columns of two D array
   srand(time(NULL));
   printf("Enter size of one-dimensional array: ");
   scanf("%d", &size);
   printf("Enter number of rows & columns of 2-D array:\n");
   scanf("%d %d", &row, &col);

   //declaring arrays
   int arr[ size ];
   
   //2-D array
   int arr2D[ row ][ col ]; 

   //one dimensional array
   for ( i = 0; i < size; ++i){
      arr[ i ] = rand() % 100 + 1;
   }

   //two dimensional array
   for ( i = 0; i < row; ++i){
      for ( j = 0; j < col; ++j){
         arr2D[ i ][ j ] = rand() % 100 + 1;
      }
   }

   //printing arrays
   printf("One-dimensional array:\n");
   
   //oneDArray( size, arr );
   for ( i = 0; i < size; ++i)
   printf("a[%d] : %d\n", i, arr[ i ]);
   printf("Two-dimensional array:\n");
   
   //twoDArray( row1, col1, arr2D );
   for ( i = 0; i < row; ++i ){
      printf("\n");
      for ( j = 0; j < col; ++j )
         printf("%5d", arr2D[i][j]);
   }
}

Output

Enter size of one-dimensional array: 5
Enter number of rows & columns of 2-D array:
4 4
One-dimensional array:
a[0] : 95
a[1] : 93
a[2] : 4
a[3] : 52
a[4] : 68
Two-dimensional array:

   92   19   79   23
   56   21   44   98
    8   22   89   54
   93    1   63   38

Jagged array

A jagged array is a collection of two or more arrays of similar data types of variable length. In C, the concept of jagged array is implemented with the help of pointers of arrays

A jagged array is represented by the following figure −

A Jagged Array

We declare three one−dimensional arrays of different sizes, and store their pointers in an array of pointers, that acts as a jagged array.

Example

#include <stdio.h>

int main(){
   int a[] = {1,2};
   int b[] = {3,4,5};
   int c[] = {6,7,8,9};
   int l1=sizeof(a)/sizeof(int), 
   l2=sizeof(b)/sizeof(int), 
   l3=sizeof(c)/sizeof(int);
   int *arr[] = {a,b,c};
   int size[] = {l1, l2, l3};
   int *ptr;
   int i, j, k=0;

   for (i=0; i<3; i++){
      ptr = arr[i];
      for (j=0; j<size[k]; j++){
         printf("%d\t", *ptr);
         ptr++;
      }
      printf("\n");
      k++;
      arr[i]++;
   }

   return 0;
}

Output

1       2
3       4       5
6       7       8       9
Advertisements