Arrays

An Array is group similar data of one particular data types, the array's elements are continuous in nature when the array is declared the location of the array elements is continuous in memory.

advantage of the array is we can assess the similar type of data by using only one variable and as the elements of an array are continuous in memory we can assess the element just by knowing the address of the first element we will see that in pointers.

We access the elements of the array by the index in c programming the index of the array starts from 0 to N-1,  where  N is the length of the array.

For example, if we want to access the first element of the array then we can use array[ 0 ],  and array[ 4 ] gives us the fifth element of the array. here the 0 and the 4 is the index for the first and the fifth element of the array.


Declaration of array:


Declaration of the array is the same as declaring a variable of any other data type, but in the case of an array we need to mention the no of elements in the array in the braces, we can declare an array by the following syntax

data_type  arrayName[size]

Example:

  1. int arr[ 4 ] ;

  2. float array1[ 3 ] ;


The first one is declares the array of integer data type with the size of 4 elements means it can store the 4 data of integer type and the name of the array is arr.

The second one is declares the array of float data type with the size of 3 elements means it can store the 3 data of float type and the name of the array is array1.

When we declare an array, some garbage value is assigned to all the elements and we can assign 0 to all elements at declaration time by using

data_type  arrayName[size] = { 0 } or  data_type  arrayName[size] = {  }.

In the time of declaration array, it is not mandatory to mention the size of the array, it done letter by the compiler when compiling the code, the array is declared according to the values assigned to it at the time. Arrays are static in nature we can't change the size of the array in your program.




Declaration of array with initializing elements:


In this method with declaring the element we also assign values to them. if we don't specify the size of the array in the declaration then the compiler declares array according to values provided in the declaration let's see the examples

1 int a[ 4 ] = { 1,2,3,4 };

It declares an array of four integers and assigns values to them as specified in curly braces means a[ 0 ] to 1, a[ 1 ] to 2 ... a[ 3 ] to 4.

2 int arr[  ] = { 12, 15 , 16 , 40 , 13 , 15 };

It declares an array of 6 integers because six values are specified in braces and assign values to them as specified in curly braces.

3 int arr[ 5 ] = { 1 , 2 }

It declares an array of 5 integers and assigns values to them as specified in curly braces, here the size of arr is 5 and only two values are provided in braces at the declaration in this case the value is assigned to elements as per the no of values provided in the declaration and other elements of array assigned value as 0.

In your example, the first two elements are assigned to values that provided in the declaration, and other elements are assigned as 0. therefor arr is look like the same as [ 1 | 2 | 0 | 0 | 0 ].

int arr[ 3 ] = { 2, 3, 5, 6 }

This declaration gives a warning in c and error in c++, we can't assign no of values more than the size of the array.


Initializing elements of array:


As we have seen earlier that we can initialize the array at the time of the declaration of the array. we can also initialize an array by using the index of the array elements in this method we assign values to the elements of the array individually. let's learn that by the example...

int arr[ 4 ];

// now initializing array elements

arr[ 0 ] = 12;

arr[ 1 ] = 0;

arr [ 2 ] = 43 ;

arr [ 3 ] = arr [ 0 ];