Array
What is an Array?
An array is a collection of similar data elements stored at contiguous memory locations. It is the simplest data structure where each data element can be accessed directly by only using its index number.
For instance, if we want to store the marks scored by a student in 5 subjects, then there’s no need to define individual variables for each subject. Rather, we can define an array that will store the data elements at contiguous memory locations.
Array marks[5] define the marks scored by a student in 5 different subjects where each subject’s marks are located at a particular location in the array, i.e., marks[0] denote the marks scored in the first subject,?marks[1]?denotes the marks scored in 2nd subject and so on.
Need of using Array –
In programming, most of the cases need to store a large amount of data of a similar type. We need to define numerous variables to store such a huge amount of data. While writing the programs, it would be very tough to memorize all variable names. Instead, it is better to define an array and store all the elements in it.
领英推荐
Advantages of Array
Accessing Elements in an Array –?
To access any element of an array, we need the following details:
The address of any element of a 1D array can be calculated by using the below-given formula:
Byte address of an element A[i] = base address + size * (i – first index)