Arrays in C programming


Arrays are the collection of the data items and these items have same data type. Arrays are widely used data structure in C Programming language.  

  • Array can only one data type elements.
  • Array has contiguous memory allocation.
  • In array we can access elements randomly.
  • In C programming array indexing always starts from 0.                                                                                         

why we use Array in C Programming:

For understand "why we use array in C Programming " lets take an example : Suppose you have 100 integer type variables and your task is make a C programming for finding sum of these 100 integer type of variables.In this case you have two options:

1.Without Array:

In this method you need to define 100 variables in your program that makes program very complex and long.


2.With Array:

In this method only we need to define one array of length 100. It makes program 100 times smaller and understandable .

So I think you guys understand why we use Arrays in C programming language.



Declaration of array in C programming:

syntax:   Data_type  Array_name[Array_size];

 
There are mainly three things in array declaration :
  1. Data_type
  2. Array_name
  3. Array_size
  1. Data_type: Here Data_type  is the type of data we want to store in the array.

  2. Array_name: Array name can be anything expect the keyword defined in the C programming language. 

  3. Array_size: Array_size is the size of array. here we can define the how many element can be in array.

 
 

Picture visual of Array in C programming:



Access elements in Array:

we can access array element by using the index number of respective element.That feature makes array more reliable.
we can perform many of the operation by using the index number like remove element, add element, replace element etc.

syntax for accessing elements of array in C programming:

Array_name[index_number]

For example:

Suppose we have an array name as My_array. Array have four elements and we want to access the first element of the My_array.

My_array[4]={1,2,3,4}
My_array[0]= 1
 

Advantage of Array in C programming:

  • Array can handle more then one element of the same data type.
  • Array has contiguous memory allocation. That avoid the wastage of memory.
  • In Array we can store elements in multidimensional way. It means we can create multidimensional array.
  • We can access elements randomly by using the index number.

Disadvantages of Array in C programming:

  • In array can store only same data type of elements. we can't able to store different types of element.