1. Gyanastra
  2. Data Structures
  3. Arrays and Address Calculation
  4. Address Calculation in a One-Dimensional Array (0-based and 1-based Indexing)
Free lesson, no account needed 5 min read 13 practice questions after it
Asked in past papers11 times
  • GATE CSE20192017201520112008
  • ISRO201720132009
  • UGC NET202320152013

Data Structures · Arrays and Address Calculation

Address Calculation in a One-Dimensional Array (0-based and 1-based Indexing)

Overview

Address calculation in a one-dimensional array finds the exact location in main memory where element A[i]A[i] is stored. It depends on two things: the starting index (0-based or 1-based) and the base address where the array begins.
The only point requiring care is whether indexing starts at 0 or 1.

Key concepts

  • Address calculation: Finding the exact memory location of a particular element A[i]A[i] in a one-dimensional array.
  • Starting index: Whether the array is 0-based (indices 0, 1, 2, 3, ...) or 1-based (indices 1, 2, 3, 4, 5, ...). This is the only point where you must be very careful.
  • Base address (BB): The memory location where the array starts — the address of A[0]A[0] in 0-based indexing, and the address of A[1]A[1] in 1-based indexing.
  • Elements crossed: The number of elements passed over before reaching A[i]A[i]; this equals ii for 0-based indexing and i−1i - 1 for 1-based indexing.
  • Element size: In this section each element occupies 1 byte, so each crossed element adds 1 to the address.
Data structure diagram: Address Calculation in a One-Dimensional Array (0-based and 1-based Indexing)
Figure. Address Calculation in a One-Dimensional Array (0-based and 1-based Indexing)

How it works

  1. To reach an element A[i]A[i], count how many elements you cross before reaching it.
  2. 0-based indexing: to reach element 2, you cross exactly two elements (elements 0 and 1), so the number of elements crossed equals the array index ii.
  3. 1-based indexing: to reach element 2, you cross only one element (element 1), which equals i−1i - 1.
  4. Add the number of elements crossed to the base address (with 1-byte elements) to get the address of A[i]A[i].
  5. If the base is 0, the address of element ii is simply ii (0-based, 1-byte elements); if the base is 100, the address is 100+i100 + i (0-based) or 100+(i−1)100 + (i - 1) (1-based).

Formulas

  • With each element occupying 1 byte:

  • 0-based indexing: Address of A[i]=B+i\text{Address of } A[i] = B + i

    • BB = base address of the array (address of A[0]A[0])
    • ii = index of the element
  • 1-based indexing: Address of A[i]=B+(i−1)\text{Address of } A[i] = B + (i - 1)

    • BB = base address of the array (address of A[1]A[1])
    • ii = index of the element
  • Reason: the number of elements crossed before reaching A[i]A[i] is ii (0-based) or i−1i - 1 (1-based), and with 1-byte elements each crossed element adds 1 to the address.

Worked examples

Example 1: 0-based indexing, base = 0, element size = 1 byte

  • Array indices: 0, 1, 2, 3.
  • Find address of A[2]A[2].
  • Elements crossed to reach A[2]A[2] = 2 (elements 0 and 1).
  • Address = 0+2=20 + 2 = 2.

Example 2: 0-based indexing, base = 100, element size = 1 byte

  • Find address of A[2]A[2].
  • Elements crossed = 2.
  • Address = 100+2=102100 + 2 = 102.
  • General: address of A[i]=100+iA[i] = 100 + i.

Example 3: 1-based indexing, base = 100, element size = 1 byte

  • Array indices: 1, 2, 3, 4, 5.
  • Find address of A[2]A[2].
  • Elements crossed to reach A[2]A[2] = 1 (only element 1), i.e. i−1=2−1=1i - 1 = 2 - 1 = 1.
  • Address = 100+(2−1)=101100 + (2 - 1) = 101.
  • General: address of A[i]=100+(i−1)A[i] = 100 + (i - 1).

Comparison

Property0-based indexing1-based indexing
First index01
Elements crossed to reach A[i]A[i]iii−1i - 1
Address of A[i]A[i] (base BB, 1-byte elements)B+iB + iB+(i−1)B + (i - 1)
Address of A[2]A[2] when B=100B = 100102101

Revision summary

  • The only difference between 0-based and 1-based indexing is the starting index; this is where you must be very careful.
  • Number of elements crossed to reach A[i]A[i]: ii for 0-based, i−1i - 1 for 1-based.
  • Address of A[i]A[i] = base address + number of elements crossed (when each element is 1 byte).
  • Shortcut: Do not memorise the formulas — derive them on the go from the count of elements crossed; they are very simple.
  • The base address is the memory location where the array starts: address of A[0]A[0] in 0-based indexing, address of A[1]A[1] in 1-based indexing.

Previous-year exam insights

  • General address formula for a 1-D array with lower bound LB, base address B and element size W: Address(A[i]) = B + (i − LB) × W;
    for 0-based LB = 0 and for 1-based LB = 1.
    Asked inISRO 2009GATE 2019
  • In C, arr + k is a pointer to arr[k] (pointer arithmetic scales by sizeof(element)), and p[i] is identical to *(p + i);
    so ip = arr + 4 followed by ip[1] gives arr[5].
    Asked inGATE 2019GATE 2015GATE 2011
  • Subtracting two pointers into the same array, p - q, gives the number of elements between them (an integer, not a byte offset), and adding an integer to a char pointer advances it by that many characters.
    Asked inGATE 2017ISRO 2017GATE 2011
  • For a 2-D array stored in row-major order, Address(A[i][j]) = B + ((i − LB1) × N2 + (j − LB2)) × W, where N2 is the number of columns;
    in column-major order the roles of rows and columns are interchanged.
  • Array name used as a function argument decays to a pointer to its first element, so the array is passed by reference and modifications inside the function affect the caller's array; sizeof(arr) inside a function then gives the pointer size, not the array size.
    Asked inGATE 2008
  • Pointer subtraction is valid only between pointers into the same array; the result is the number of elements between them, and dereferencing a pointer outside the array bounds is undefined behaviour.
    Asked inGATE 2019GATE 2015
Pages 1–2 of 2