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 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 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 (): The memory location where the array starts — the address of in 0-based indexing, and the address of in 1-based indexing.
- Elements crossed: The number of elements passed over before reaching ; this equals for 0-based indexing and for 1-based indexing.
- Element size: In this section each element occupies 1 byte, so each crossed element adds 1 to the address.

How it works
- To reach an element , count how many elements you cross before reaching it.
- 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 .
- 1-based indexing: to reach element 2, you cross only one element (element 1), which equals .
- Add the number of elements crossed to the base address (with 1-byte elements) to get the address of .
- If the base is 0, the address of element is simply (0-based, 1-byte elements); if the base is 100, the address is (0-based) or (1-based).
Formulas
-
With each element occupying 1 byte:
-
0-based indexing:
- = base address of the array (address of )
- = index of the element
-
1-based indexing:
- = base address of the array (address of )
- = index of the element
-
Reason: the number of elements crossed before reaching is (0-based) or (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 .
- Elements crossed to reach = 2 (elements 0 and 1).
- Address = .
Example 2: 0-based indexing, base = 100, element size = 1 byte
- Find address of .
- Elements crossed = 2.
- Address = .
- General: address of .
Example 3: 1-based indexing, base = 100, element size = 1 byte
- Array indices: 1, 2, 3, 4, 5.
- Find address of .
- Elements crossed to reach = 1 (only element 1), i.e. .
- Address = .
- General: address of .
Comparison
| Property | 0-based indexing | 1-based indexing |
|---|---|---|
| First index | 0 | 1 |
| Elements crossed to reach | ||
| Address of (base , 1-byte elements) | ||
| Address of when | 102 | 101 |
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 : for 0-based, for 1-based.
- Address of = 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 in 0-based indexing, address of 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