Pointer Arithmetic & Multi-Dimensional Array Subscripts
“p + 1 scales by sizeof(*p), *(A + i) vs A[i], and 2D matrix row-major offsets.”
1.The Fundamental Mechanics of Pointer Arithmetic
§ 2.1.A pointer variable stores a memory address. In C, adding an integer k to a pointer p of type T^* does NOT add k bytes! It adds: k × sizeof(T) bytes.
Pointer Scaling Rule:
$Address(p + k) = Address(p) + k × sizeof(*p)$
Subscript Equivalence:
The array subscript expression A[i] is syntactically translated by the compiler as * (A + i). Because addition is commutative:
$A[i] \equiv (A + i) \equiv (i + A) \equiv i[A]$
Array Decay Rule:
In almost all C expressions, an array name of type T[N] automatically decays into a pointer of type T^* pointing to its first element (&A[0]).
The Two Exceptions to Array Decay:
1. When used as operand to sizeof: sizeof(A) yields total array capacity N × sizeof(T).
2. When used as operand to address-of &: &A yields a pointer to the entire array of type T(*)[N].
If int A[5] starts at address 1000: The numerical addresses of A and &A are identical (1000). But their types are completely different! A + 1 advances by 1 integer: 1000 + 4 = 1004. But &A + 1 advances by the ENTIRE array: 1000 + (5 × 4) = 1020!
2. GATE Worked Numerical: 2D Array Pointer Dereferencing
In a 2D array int M[3][4], M has type pointer-to-array of 4 ints (int(*)[4]). Dereferencing *M yields type int* pointing to M[0][0]!
Given declaration: int M[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} };
Assume base address of M is 2000 and sizeof(int) = 4 bytes.
Evaluate the values of:
1. ((M + 1) + 2)
2. (M + 1)
3. ((M + 2))
Step 1: Evaluate ((M + 1) + 2):
M has type int()[4]. M + 1 skips 1 full row of 4 ints: address = 2000 + 4 4 = 2016.
(M + 1) dereferences to type int, pointing to M[1][0] at 2016.
*(M + 1) + 2 advances 2 integer elements in row 1 -> points to M[1][2] at 2016 + 8 = 2024.
((M + 1) + 2) dereferences element M[1][2] = 7.
Step 2: Evaluate (M + 1):
M decays to pointer to M[0][0]. M + 1 points to M[0][1].
(M + 1) = M[0][1] = 2.
Step 3: Evaluate ((M + 2)):
M + 2 points to row 2. *(M + 2) points to M[2][0].
((M + 2)) = M[2][0] = 9.
Primary Academic References & Exam Sources
Every concept in Notes From a B.Tech Brain is synthesized from authoritative academic texts, seminal peer-reviewed research, verified exam archives, and renowned university lectures.
Curated for clarity, rigorous numerical solving, and semester excellence:
Systematic beginner-to-advanced C with clear examples.
Clear step-by-step C explanations — great for semester lab preparation.