Arrays are nothing but a bulk storage of a single data type in a single name. Suppose in a program, we need to store months of a year, then we take twelve different variables. Instead of taking twelve different variables and taking a risk to maintain them, we can declare all the twelve under one name i.e., months, by declaring it as an array.
An array can have any number of elements. All these elements are given an index with a subscript to the variable.
Declaration:
This indexing of array starts from 0 and extends up-to n-1 where n is the number in the subscript. All these elements of array are given a sequential memory unlike the normal variables. Hence, with the knowledge of the base index address i.e., the address of the first value of the array, we can move through the entire array.
There are two types of arrays.
An array can have any number of elements. All these elements are given an index with a subscript to the variable.
Declaration:
data_type variable_name[number_of_elements];The above shown is the syntax of the declaring arrays. The array may be of any type. It is similar to a normal variable declaration. The difference lies with the subscript. In normal declaration, we just take only one variable. In arrays, it also specifies the count of number of elements that array should contain.
This indexing of array starts from 0 and extends up-to n-1 where n is the number in the subscript. All these elements of array are given a sequential memory unlike the normal variables. Hence, with the knowledge of the base index address i.e., the address of the first value of the array, we can move through the entire array.
There are two types of arrays.
1.Single Dimensional Array:
Single dimensional arrays are those arrays in which the subscript is having single dimension. These are accessed by a single subscript value and it is represented as shown below.
Example:
int a[10];
The values in this array are called by using array variable with corresponding value.
a[0], a[1].....a[9]
Initialization of Array:
We can initialize single dimensional arrays like this:
a[10]={1,2,3,4,5,6,7,8,9,10};
Accessing values:
We can access a value from the array by specifying the index as the subscript to the array variable.
Here, a[0]=1, a[3]=4 etc.
2.Multi-Dimensional Arrays:
Multi-dimensional arrays are those arrays which have multiple subscripts. Here is an example of Multi-dimensional arrays.
Example:
int b[4][5];
It will have the values to be as
b[0][0], b[0][1].....b[0][4]
b[1][0], b[1][1].....b[1][4]
...
b[3][0], b[3][1].....b[3][4]
This is the representation of the 2 dimensional array.
Initialization of Array:
For initializing the 2-dimensional array, we can specify it as
b[4][5]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
or
b[4][5]={{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15},{16,17,18,19,20}};
Accessing values:
We can access values of the array by specifying the index values like this.
b[0][0]=1, b[0][4]=5, b[2][4]=15 etc.
We can also use constant variables in the subscript which do have some value.
For example, if we have statement in the program like,
#define MAX 12
Then we can use an array declaration like this:
int arr[MAX];
However, we can't use a variable declared with a const keyword in C. We can use the constants declare by the directives only.