After we have understood what are arrays , how they are to be declared and its applications , it is time to understand how to access the elements.

Although, we store the similar data items into a group called array which is referenced to by a single name like marks, temp etc as seen previously, however in order to retrieve and access the elements we do not have a single operation.

We need to use loops for this purpose. We access the elements by incrementing the value of the index / subscript. These subscripts are integer values.

Example: Consider an integer array called rollno.

int rollno[10];   //array declaration

int i;

for (i=0; i<10; i++)

{

rollno[i] = i+1;  //accessing elements of array and assigning value to them.

}

In the above example, we are accessing initially the first element of array which is at the 0th index, hence the counter i = 0. Then we assign value to it using assignment operator. Similarly we access and assign value to each element until the last one which is located at 9th index, hence  i<10. Because if we put i<= 10; then what happens is when i =10; there is no rollno[10] as a part of the array; because the last index is 9 as array initialises from 0.

The output of the above code would be as follows:

This leads us to an important conclusion that , in order to access or do any operation on  array elements we have to use loop and perform the same operation on each element by iterating the index value.

A very essential part of arrays is to understand how the compiler knows where individual elements of array are located in memory. So, let us understand how is it done.

Memory Address calculation of array elements:

You will be surprised to know that when we use the array name, we are referring to nothing but the first byte of the array. Array name corresponds to the address of the first byte of the array.

The subscripts or index are offset / you can say the distance from the starting location of the array.  

Also, all the elements are located in consecutive memory locations. Thus using the name of array which is the base address and offset, C calculates address of all elements in run-time.

Formula to calculate address:

Address of element of a[i] = base address of array a[] +  data_type_size * (i - starting index )

Here, ‘a’ is the array, i is the index of element whose address we are calculating (offset) , data_type_size refers to the memory blocks reserved for a particular data type ; for e.g: if your array is of integer type ; each element will occupy 2 bytes. Starting index is usually 0.

For instance, correlate this calculation to your calculation of age. You just require today’s date and the date of birth in order to derive age, right ? So consider your base address is your DOB and today’s date is the offset or index. Voila ! It’s  easy to relate to it now, isn’t it !

Example:

You are given the following array :

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

Base Address  = 2000, Calculate the address of rollno[3].

Answer:

The array will be represented in memory as follows:


As we know storing int requires 2 bytes.

Hence,

data_type_size = 2

starting index = 0

base address of array rollno[] = 2000

i =3

Hence rollno[3] = 2000 + 2*(3-0) =2000 + 6 = 2006.

In the next section we shall see how to initialise ,input and assign values in array.

Report Error/ Suggestion

Related Posts:

[yuzo_views]











CopyRight © 2019

CopyRight © 2019