Loading lessons...
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]:
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]equalsA + i.- Incrementing a pointer
p++advances the memory address bysizeof(*p)bytes, not 1 byte! - For a 2D array
int M[3][4],M[i][j]is*(*(M + i) + j).
int arr[5] = {10, 20, 30, 40, 50};
int *p = arr;
printf("%d %d\n", arr[2], *(p + 2)); // Both print 303. 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
- Row-Major Order (C, C++, Java): Rows are stored one after another:
- Column-Major Order (Fortran, MATLAB): Columns are stored one after another:
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:
Column-Major:
Array Operations as Step Algorithms
Traverse LA with lower bound LB and upper bound UB:
- Set K := LB.
- Repeat steps 3-4 while K ≤ UB.
- Apply PROCESS to LA[K].
- Set K := K + 1.
- Exit.
Insert ITEM at position K in LA holding N elements:
- Set J := N.
- Repeat steps 3-4 while J ≥ K.
- Set LA[J + 1] := LA[J] (shift right).
- Set J := J − 1.
- Set LA[K] := ITEM.
- Set N := N + 1.
- Exit.
Delete the element at position K:
- Set ITEM := LA[K].
- Repeat for J = K to N − 1: set LA[J] := LA[J + 1] (shift left).
- Set N := N − 1.
- 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
| Basis | Array | Pointer |
|---|---|---|
| Meaning | A fixed block of same-type elements | A variable holding an address |
| Memory | Allocated at declaration (static) | Can point to memory allocated at run time (malloc) |
sizeof | Size of the whole array | Size of the pointer itself (4 or 8 bytes) |
| Reassignment | Array name is a constant address - a = b; is illegal | p = q; is legal |
| Initialization | int a[3] = {1,2,3}; | int *p = a; |
| Access | a[i] | *(p + i) (identical machine code) |
📝 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, calculateLOC(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 (
2372and5240).