Arrays, pointers and compact storage
Array addresses come from the number of preceding elements times element width. Pointer arithmetic uses the pointed-to type, so a pointer to a row advances differently from a pointer to an element.
7.1 Address calculation and dimensions
7.2 Array decay and pointer arithmetic
Past papers · CS 2014–2026 (23 papers): 10 questions, 16 marks
For an array of elements, most expressions convert its name to a pointer to its first element. Exceptions include sizeof and unary &. In a parameter declaration int a[], the parameter is adjusted to int *a; the callee cannot obtain the original array length using sizeof a.
For int a[R][C], a decays to int (*)[C]; a+1 skips one row, a[i]+1 one int, and &a+1 the entire two-dimensional array. int ** is a different type and representation. Arrays of pointers may point to separately allocated rows.
7.3 Array updates, sums and scan invariants
Past papers · CS 2014–2026 (23 papers): 4 questions, 7 marks · DA 2024–2026 (3 papers): 1 question, 2 marks
Arrays are mutable shared storage. A swap of with over every ordered pair swaps each off-diagonal pair twice; only one triangular half should be processed for an in-place transpose. For partial aggregate initialisation such as int a[5]={8,6};, the remaining elements are zero. A wholly uninitialised automatic array is different.
To understand a nested scan, identify its candidate set and initial accumulator. Enumerating every pair and summing each segment finds the maximum nonempty subarray sum if the starting maximum is a valid nonempty candidate. Initialising that maximum to zero permits an empty segment and changes the answer for all-negative arrays. Efficient maximum-subarray algorithms are covered in Algorithms.
For an endpoint-elimination maximum scan, keep the largest remaining candidate within a shrinking interval: compare its two endpoints and discard the smaller one until they coincide. For repeated digit extraction or index increments, make a table of the old value used and the new value stored.
7.4 Packed matrices and ordered grids
Past papers · CS 2014–2026 (23 papers): 1 question, 2 marks
When a matrix has a fixed zero pattern, store only the permitted entries. Specify whether a diagonal is included and whether the packed sequence is row or column major before calculating a position.
A diagonal matrix stores entries. A tridiagonal matrix stores for ; count its diagonal plus two adjacent diagonals. A general sparse matrix can use triples (row, column, value) with a nonzero count; the bytes depend on index and value widths, not only on nonzero count.
In a Young tableau, rows and columns are nondecreasing, and empty cells carry an infinity sentinel. After removing the top-left minimum, move the smaller finite key among the hole's existing right and lower neighbours into it. Repeat until neither neighbour holds a finite key. Reaching the last row or column alone does not end the update. The hole moves only right or down, giving at most moves in an tableau. This is a local ordered-grid update, not a binary-heap array operation.
Last-minute recall
- Row-major stride equals the product of later dimension lengths.
- Array names and pointers are not interchangeable objects.
- A pointer-to-row is not a pointer-to-pointer.
- Pointer subtraction returns element distance.
- Partial aggregate initialisation fills omitted elements with zero.
- Check whether a maximum permits an empty candidate.
- Packed-storage formulae depend on order and diagonal convention.