What are the limitations of array in C language?


Arrays are a kind of data structure that can store a fixed-size sequential collection of elements of the same type.

An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

Limitations

The limitations of an array are explained below −

  • An array which is formed will be homogeneous. That is, in an integer array only integer values can be stored, while in a float array only floating value and character array can have only characters. Thus, no array can have values of two data types.

  • While declaring an array, passing size of an array is compulsory, and the size must be a constant. Thus, there is either shortage or wastage of memory.

  • Shifting is required for insertion or deletion of elements in an array.

  • An array doesn’t check boundaries: In C language, we cannot check, if the values entered in an array are exceeding the size of that array or not.

  • Data that is entered with the subscript, exceeds the array size and will be placed outside the array. Generally, on the top of the data or the program itself.

  • This will lead to unpredictable results, to say the least. Also, there will be no error message to warn the programmer of going beyond the array size. In some cases, the program may hang.

Thus, the following program can give undesired result −

int a[10],i;
for(i=0;i<=20;i++)
a[i]=i;

Example

Following is the C program to display sum of two arrays −

 Live Demo

#include<stdio.h>
void main(){
   //Declaring array with compile time initialization//
   int array1[5],array2[5],sum[5];
   //Declaring variables//
   int i;
   //Printing O/p using for loop//
   printf("Enter the values of array1 :
");    for(i=0;i<5;i++){       printf("array1[%d] :
",i);       scanf("%d",&array1[i]);    }    printf("Enter the values of array2 :
");    for(i=0;i<5;i++){       printf("array2[%d] :
",i);       scanf("%d",&array2[i]);    }    printf("Elements in the sum of array1 and array2 are:
");    for(i=0;i<5;i++){       sum[i]=array1[i]+array2[i];       printf("%d ",sum[i]);    } }

Output

When the above program is executed, it produces the following result −

Enter the values of array1 :
array1[0] :2
array1[1] :3
array1[2] :1
array1[3] :2
array1[4] :3
Enter the values of array2 :
array2[0] :4
array2[1] :5
array2[2] :3
array2[3] :2
array2[4] :1
Elements in the sum of array1 and array2 are: 6 8 4 4 4

Updated on: 13-Mar-2021

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements