Arrays of Variable Length (or) Variable length arrays - C-Tutorial

Latest

Friday, 11 November 2016

Arrays of Variable Length (or) Variable length arrays

Arrays of Variable Length (or) Variable length arrays

Variable-length automatic arrays are allowed in C99, C90 mode.
Variable length arrays is a feature where we can allocate an auto array (on stack) of variable size.

  • C supports variable sized arrays from C99 standard. For example, the below program compiles and runs fine in C.

In variable length arrays dimensions of the array can also be specified by an valid expression.
Values to the arguments can be specified at run time.

/* Example-1 program for variable length array */

#include<stdio.h>
#include<conio.h>

void action(int);
main()
{
int n;
clrscr();
printf("\n Enter n value:");
scanf("%d",&n);
action(n);
}

void action(int n)
{
int i, a[n];
printf("\n Enter the array elements: ");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
printf("\n The given array elements are: ");
for(i=0; i<n; i++)
printf("%d",a[i]);
}

No comments:

Post a Comment