Accessing array element in C

Written by

Pooja Rao

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 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.

An array element can be accessed through an index number.

Example: Consider an integer array called rollno.

#include <stdio.h>

int main() {
  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.
  }

  for (i = 0; i < 10; i++) {
    printf("rollno[%d] = %d\n", i, rollno[i]);
  }

  return 0;
}

Output

rollno[0] = 1
rollno[1] = 2
rollno[2] = 3
rollno[3] = 4
rollno[4] = 5
rollno[5] = 6
rollno[6] = 7
rollno[7] = 8
rollno[8] = 9
rollno[9] = 10

In the above example, we are accessing initially the first element of the array which is at the 0th index, hence the counter i = 0. Then we assign value to it using the 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,

# Declare and initialize variables
data_type_size = 2  # The size of the data type stored in the array, in bytes
starting_index = 0  # The index of the first element in the array
base_address = 2000  # The memory address of the first element in the array
i = 3  # The index of the current element in the array that we are working with

# Calculate the memory address of the current element
current_address = base_address + (i - starting_index) * data_type_size

# Output the memory address and value of the current element
print(f"The memory address of rollno[{i}] is {current_address}.")
print(f"The value of rollno[{i}] is stored at memory address {current_address}.")

In this example, the memory address of rollno[3] is calculated as follows:

memory_address = 2000 + (3 - 0) * 2 = 2000 + 6 = 2006

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

Accessing array element in C