Lesson 3 +30 XP

Arrays, Pointers & Multidimensional Address Calculation

Arrays, Pointers & Multidimensional Address Calculation

An Array is a fixed-size, homogeneous collection of elements stored in contiguous memory locations. Because memory is contiguous, any element can be accessed in O(1) time via simple offset arithmetic.

1. 1D Array Length & Address Formula

  • Length: Length = UB - LB + 1 (where LB is Lower Bound and UB is Upper Bound).
  • Address of A[K]:

LOC(A[K]) = Base(A) + w × (K - LB)
where w is the size of each element in bytes (e.g., sizeof(int) = 4).

Example: Given A(2:22) with Base(A) = 1000, w = 4:

  • Length = 22 - 2 + 1 = 21 elements.
  • LOC(A[10]) = 1000 + 4 × (10 - 2) = 1032.

2. Arrays and Pointers in C

In C, an array name acts as a constant pointer to its first element (&A[0]):

  • A[i] is strictly equivalent to *(A + i) and &A[i] equals A + i.
  • Incrementing a pointer p++ advances the memory address by sizeof(*p) bytes, not 1 byte!
  • For a 2D array int M[3][4], M[i][j] is *(*(M + i) + j).
C
int arr[5] = {10, 20, 30, 40, 50};
int *p = arr;
printf("%d %d\n", arr[2], *(p + 2)); // Both print 30

3. 2D Array Mapping (Row-Major vs Column-Major)

Let a 2D array be A[LB1 : UB1, LB2 : UB2] with:

  • L1 = UB1 - LB1 + 1 (number of rows), E1 = i - LB1
  • L2 = UB2 - LB2 + 1 (number of columns), E2 = j - LB2
  1. Row-Major Order (C, C++, Java): Rows are stored one after another:

LOC(A[i][j]) = Base(A) + w × [L2 · E1 + E2]

  1. Column-Major Order (Fortran, MATLAB): Columns are stored one after another:

LOC(A[i][j]) = Base(A) + w × [L1 · E2 + E1]

2D Worked Example: Given A(1:10, 1:5), Base = 2000, w = 2, find LOC(A[4][3]):

  • L1 = 10, L2 = 5, E1 = 3, E2 = 2.
  • Row-Major: 2000 + 2 × (5 · 3 + 2) = 2000 + 34 = 2034.
  • Column-Major: 2000 + 2 × (10 · 2 + 3) = 2000 + 46 = 2046.

4. General N-D & 3D Array Worked Example (CIC-209)

For an N-dimensional array, Lk = UBk - LBk + 1 and Ek = Kk - LBk:

  • Row-Major: accumulate left-to-right: ((E1 * L2 + E2) * L3 + E3)...
  • Column-Major: accumulate right-to-left: ((EN * L(N-1) + E(N-1)) * ... * L1 + E1)

3D Worked Example: Given B(1:8, -5:5, -10:5) with Base(B) = 400 and w = 4, find LOC(B[3, 3, 3]):

  • L1 = 8 - 1 + 1 = 8, E1 = 3 - 1 = 2
  • L2 = 5 - (-5) + 1 = 11, E2 = 3 - (-5) = 8
  • L3 = 5 - (-10) + 1 = 16, E3 = 3 - (-10) = 13

Row-Major:

400 + 4 × ((2 · 11 + 8) · 16 + 13) = 400 + 4 × (30 · 16 + 13) = 400 + 4 × 493 = 2372

Column-Major:

400 + 4 × ((13 · 11 + 8) · 8 + 2) = 400 + 4 × (151 · 8 + 2) = 400 + 4 × 1210 = 5240

Array Operations as Step Algorithms

Traverse LA with lower bound LB and upper bound UB:

  1. Set K := LB.
  2. Repeat steps 3-4 while K ≤ UB.
  3. Apply PROCESS to LA[K].
  4. Set K := K + 1.
  5. Exit.

Insert ITEM at position K in LA holding N elements:

  1. Set J := N.
  2. Repeat steps 3-4 while J ≥ K.
  3. Set LA[J + 1] := LA[J] (shift right).
  4. Set J := J − 1.
  5. Set LA[K] := ITEM.
  6. Set N := N + 1.
  7. Exit.

Delete the element at position K:

  1. Set ITEM := LA[K].
  2. Repeat for J = K to N − 1: set LA[J] := LA[J + 1] (shift left).
  3. Set N := N − 1.
  4. Exit.

Inserting or deleting at the end is O(1); at the front it moves all N elements, O(n).

Worked Examples from the Notes

Length of an array with index range LB : UB is UB − LB + 1.

Example 1. A(2:22), Base(A) = 300, w = 4 bytes.

  • Number of elements = 22 − 2 + 1 = 21.
  • LOC(A[15]) = Base + w(K − LB) = 300 + 4(15 − 2) = 352.
  • A[25] is invalid - 25 lies outside 2…22.

Example 2. B(20) (C-style 0…19 or 1…20) has 20 elements.

Example 3. A(−2:2, 2:22) has (2 − (−2) + 1) × (22 − 2 + 1) = 5 × 21 = 105 elements.

Example 4. B(1:8, −5:5, −10:5), Base = 300, w = 4. Find B[3, 3, 3].

  • Lengths: L₁ = 8, L₂ = 11, L₃ = 16 → 8 × 11 × 16 = 1408 elements.
  • Effective indices: E₁ = 3 − 1 = 2, E₂ = 3 − (−5) = 8, E₃ = 3 − (−10) = 13.
  • Row-major: offset = (E₁·L₂ + E₂)·L₃ + E₃ = (2·11 + 8)·16 + 13 = 493 → LOC = 300 + 4 × 493 = 2272.
  • Column-major: offset = (E₃·L₂ + E₂)·L₁ + E₁ = (13·11 + 8)·8 + 2 = 1210 → LOC = 300 + 4 × 1210 = 5140.

Arrays vs Pointers

BasisArrayPointer
MeaningA fixed block of same-type elementsA variable holding an address
MemoryAllocated at declaration (static)Can point to memory allocated at run time (malloc)
sizeofSize of the whole arraySize of the pointer itself (4 or 8 bytes)
ReassignmentArray name is a constant address - a = b; is illegalp = q; is legal
Initializationint a[3] = {1,2,3};int *p = a;
Accessa[i]*(p + i) (identical machine code)
🎯 END-TERM EXAM PREP

📝 IPU End-Term Exam Corner (5-10 Marks)

  • Q1. Derive the address calculation formula for Row-Major and Column-Major order in a 2D array A[LB1:UB1, LB2:UB2]. (5 Marks)
  • Write: Define L1, L2, E1, E2, state both formulas clearly, and show a 2D numeric example.
  • Q2. Given 3D array B(1:8, -5:5, -10:5), Base = 400, w = 4, calculate LOC(B[3,3,3]) in both Row-Major and Column-Major order. (7.5 Marks)
  • Write: Always compute L1, L2, L3 and E1, E2, E3 first, substitute into the formula, and box both final addresses (2372 and 5240).