Array

An array is a collection of similar data elements. These data elements have the same data type. The elements of the array are stored in consecutive memory locations and are referenced by an index (also known as the subscript). The subscript is an ordinal number which is used to identify an element of the array.

Ex: int A[5];

Learn more about Array here

Calculating the Address of 1D Array Elements

  • The array name is a symbolic reference to the address of the first byte of the array. When we use the array name, we are actually referring to the first byte of the array.
  • The subscript or the index represents the offset from the beginning of the array to the element being referenced. That is, with just the array name and the index, C can calculate the address of any element in the array.
  • Since an array stores all its data elements in consecutive memory locations, storing just the base address, that is the address of the first element in the array, is sufficient. The address of other data elements can simply be calculated using the base address.
  • The formula to perform this calculation is,

Address of A[k] = Base_Address(A) + w(k – Lower_Bound)

Here, A is the array, k is the index of the element of which we have to calculate the address, Base_Address is the base address of the array A, and w is the size of one element in memory, for example, size of int is 2 bytes.

Address calculation in 1d array : Example

Example 1 : Given an array int marks[] = {99,67,78,56,88,90,34,85}, calculate the address of marks[4] if the base address = 1000.

Solution :

address calculation in 1d array with example

We know that storing an integer value requires 2 bytes, therefore, its size is 2 bytes.

Address of A[k] = Base_Address(A) + w(k – Lower_Bound)

marks[4] = 1000 + 2(4 – 0)
= 1000 + 2(4)
= 1008 [Ans]

Example 2 : Base address of an array B[1300 : 1900] as 1020 and size of each element of array is 2 bytes in the memory. Find the address of B[1700].

Solution : 

The given values are: Base_Address = 1020, Lower_Bound = 1300, w = 2, k = 1700

Address of A[k] = Base_Address(A) + w(k – Lower_Bound)

B[1700] = 1020 + 2 * (1700 – 1300)
= 1020 + 2 * 400
= 1020 + 800
= 1820 [Ans]

Calculating the Length of an Array

The length of an array is given by the number of elements stored in it. The general formula to calculate the length of an array is

Length = upper_bound – lower_bound + 1

where upper_bound is the index of the last element and lower_bound is the index of the first element in the array.

In this article, we have discussed about address calculation in 1d array with examples. You can learn more about an array here.