What is Array in C ?

  • Arrays are defined as collection of similar type of data items that is stored at consecutive blocks of memory locations.
  • Arrays are deprived data types which can store primitive data types such as int, char, float, double etc.
  • Arrays are also used to store pointers and structures in C.
  • Arrays are considered as simplest data structure where each data element can be randomly accessed using its index numbers.
  • Array index starts from 0 to n number of elements.
  • Array Size once fixed cannot be changed in the program.

Properties of Array

  • Each element in the array is of the same data type of which the array is declared.
  • Array carries the same size int = 4bytes.
  • Elements in array are arranged in a consecutive blocks of memory at memory locations.
  • The first element in the array is stored at the smallest memory location.
  • Elements of the array can be randomly accessed since we calculate the index of each element of the array with the given base address and the size of the element.

Advantages of Array

  • Arrays use less code for code effectiveness.
  • By using the  loop, we can retrieve the elements of an array easily.
  • To sort the elements of the array, we need a few lines of code only.
  • We can access any element randomly using the array.

Declaration of C Array

We can declare an array in the c language as below

data_type array_name[array_size];  

Now, let us see the example to declare the array.

int marks[5];  

Here, int is the data_type, marks are the array_name, and 5 is the array_size.

Initialization of C Array

The simplest way to initialize an array is by using the index of each element. We can initialize each element of the array by using the index. Consider the following example.

marks[0]=80;//initialization of array  

Pictorial representation of array initialization

Leave a Reply

Your email address will not be published.

You May Also Like