Home > Backend Development > C++ > What are the limitations of arrays in C language?

What are the limitations of arrays in C language?

王林
Release: 2023-08-25 17:21:17
forward
1832 people have browsed it

What are the limitations of arrays in C language?

An array is a data structure that can store a fixed-size sequential collection of elements of the same type.

Arrays are used to store collections of data, but it is more useful to think of arrays as collections of variables of the same type.

Limitations

The following are the limitations of arrays:

  • The array formed will be homogeneous. That is to say, only integer values ​​can be stored in an integer array, only floating point values ​​can be stored in a floating point array, and only characters can be stored in a character array. Therefore, an array cannot have values ​​of both data types at the same time.

  • When declaring an array, passing the size of the array is mandatory, and the size must be a constant. Therefore, there may be insufficient or wasted memory.

  • Shift operations are required when inserting or deleting elements from an array.

  • Array does not check bounds: In C language, we cannot check whether the value entered in the array exceeds the size of the array.

  • Data entered using subscripts exceeds the array size and will be placed outside the array. Usually on top of the data or the program itself.

  • This will lead to unpredictable results, to say the least. Moreover, there is no error message to warn the programmer that the array size has been exceeded. In some cases, the program may hang.

Therefore, the following program may produce undesired results:

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

Example

The following is a C program that displays the sum of two arrays:

Real time demonstration

#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 :</p><p>");
   for(i=0;i<5;i++){
      printf("array1[%d] : </p><p>",i);
      scanf("%d",&array1[i]);
   }
   printf("Enter the values of array2 :</p><p>");
   for(i=0;i<5;i++){
      printf("array2[%d] :</p><p>",i);
      scanf("%d",&array2[i]);
   }
   printf("Elements in the sum of array1 and array2 are:</p><p> ");
   for(i=0;i<5;i++){
      sum[i]=array1[i]+array2[i];
      printf("%d ",sum[i]);
   }
}
Copy after login

Output

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

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
Copy after login

The above is the detailed content of What are the limitations of arrays in C language?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template