PROGRAMMING IN C/Student Engineering Notebook
Page 03·notes
POINTERS, ARRAY DECAY & FUNCTION POINTERS—~30 MIN READ

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

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].

notice this:— A vs &A: The Crucial Type Difference

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!

WORKED EXAM ANALYSIS · STEP-BY-STEP DERIVATION

2. GATE Worked Numerical: 2D Array Pointer Dereferencing

⚠️ Pitfall Warning:

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.

WORKED NUMERICAL · STUDENT DERIVATION✎ Key Takeaway: Pointer arithmetic scales by the size of what the pointer points to. Always work out that type first.

Worked example: pointers into a 2D array

Problem Statement:
int a[3][4]; with 4-byte ints, and a starting at address 1000. Find the addresses of a + 1 and (a + 1) + 2, and what (*(a + 1) + 2) is.
Step 1: The type of a

In expressions a decays to int (*)[4], a pointer to a row of 4 ints, so steps are 16 bytes.

Step 2: a + 1

One row on.

1000 + 16
↳ Result: 1016
Step 3: *(a + 1) + 2

(a + 1) is row 1, an int; adding 2 moves 2 ints, 8 bytes.

1016 + 8
↳ Result: 1024
Step 4: Dereference

((a + 1) + 2) is the same as a[1][2].

Final Answer:a + 1 = 1016; *(a + 1) + 2 = 1024; the value is a[1][2]
STUDENT PRACTICE · CONCEPT DRILL

Practice: test yourself

Original practice questions in the GATE style, with worked explanations. Not past-paper questions.

Problem 1 · FOUNDATION

int a[3][4]; with 4-byte ints. What is sizeof(a)?

bytes
Problem 2 · GATE-LEVEL

int a[3][4]; int *p = &a[0][0]; Which element is *(p + 5)?

Problem 3 · FOUNDATION

int arr[] = {10, 20, 30, 40}; int *p = arr + 1; What is *(p + 2)?

SOURCE TRANSPARENCY & CITATIONS

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 University Lecture Series
Expert C Programming: Deep C Secrets ·
LECTURE SERIES
RECOMMENDED YOUTUBE EDUCATORS & FULL-COURSE PLAYLISTS

Curated for clarity, rigorous numerical solving, and semester excellence:

Neso Academy · C Programming Full Course↗Instructor: Neso Academy · Covers: Data types, pointers, arrays, functions, recursion, file I/O

Systematic beginner-to-advanced C with clear examples.

Open YouTube↗
Jenny's Lectures CS/IT · C Programming↗Instructor: Jenny's Lectures · Covers: Arrays, strings, pointers, structures, file handling, preprocessor

Clear step-by-step C explanations — great for semester lab preparation.

Open YouTube↗