Array in C.

Array in C

  • Array in C language is a collection or group of elements (data). 
  • All the elements of c array are homogeneous (similar). It has contiguous memory location.
  • C array is beneficial if you have to store similar elements. Suppose you have to store marks of 50 students, one way to do this is allotting 50 variables. So it will be typical and hard to manage. 
  • For example we can not access the value of these variables with only 1 or 2 lines of code.
  • Another way to do this is array. By using array, we can access the elements easily. Only few lines of code is required to access the elements of array.
https://mskuthar.blogspot.com/


Advantage of C Array


  1. Code Optimization: Less code to the access the data.
  2. Easy to traverse data: By using the for loop, we can retrieve the elements of an array easily.
  3. Easy to sort data: To sort the elements of array, we need a few lines of code only.
  4. Random Access: We can access any element randomly using the array.

Disadvantage of C Array


  1. Fixed Size: Whatever size, we define at the time of declaration of array, we can't exceed the limit. So, it doesn't grow the size dynamically like Linked-List which we will learn later.

How to declare an array?

dataType arrayName[arraySize];
  • For example,
float mark[5];
  • Here, we declared an array, mark, of floating-point type. And its size is 5. Meaning, it can hold 5 floating-point values.
  • It's important to note that the size and type of an array cannot be changed once it is declared.
More about array


Post a Comment