Array

Learn about arrays, an essential part of your programming toolbox.

What is an array?

  • An array is a contiguous area of memory.
  • This contiguous area contains elements of equal size.
  • These elements are indexed by contiguous integers.

    In different languages, arrays are indexed differently. In some languages, array indexing starts at 1. In others, it starts at 0. In Python, array indexing starts at 0.

  • Arrays provide constant-time access to read and write any item in an array. This is possible because of the properties of the array. Each element of the array is of the same size. So, by taking the address of the first element, we can get the address of the ithi^{th} element by using a calculation.

    ith index element address = array first element address + size of elements * (i – first index of array)

    The above calculation is just to help you understand the concept. We don’t need to perform it at runtime. We directly access the element, and the calculation happens internally.

    Quiz: Understanding array address calculation

    Q

    We have an array of first element addresses as 2020. The size of the element is 16, and the first index is 0. What is the address of the 6th element?

    A)

    2036

    B)

    2100

    C)

    2084

    D)

    2116

    Explanation: The correct answer is 21002100. Since our array is starting from index 00 ...