NBC 103 • Unit 316 min readHigh Exam Frequency

Pointers with Arrays, Pointer Decay & Function Pointers

Unit 3: Arrays, Pointers & String ManipulationProblem Solving Using C

👨‍🏫 Professor's Mental Model: The Train Engine Hitch vs Independent Remote Drone

Array ka naam (arr) train ke stationary engine jaisa hai: wo hamesha pehle dibbe ke track par bandha rehta hai (Constant Pointer - arr++ nahi kar sakte!). Lekin ek pointer variable (ptr) ek udte hue remote drone jaisa hai: aap use kisi bhi dibbe ke upar bhej sakte ho (ptr++ is legal)! Jab array function me pass hota hai, toh pura train nahi jata, bas engine ka address jata hai (Pointer Decay)!

Academic Lecture Notes & Solved Study Pages

Unit 3 • Core Concepts, Step-by-Step Proofs & Notebook Solutions

3 Notebook Pages
NOTEBOOK PAGE 1 OF 3

1. The Equivalence Principle: Arrays and Pointers

In C, arrays and pointers are deeply intertwined through the Equivalence Principle:

The array name `arr` decays into a constant pointer to its first element: `arr == &arr[0]`.
Subscript Notation vs Pointer Dereferencing:

`arr[i]` is identical to `*(arr + i)`

`i[arr]` is also syntactically legal in C because addition is commutative: `*(i + arr)`!

Crucial Distinction:
- Array name is a CONSTANT POINTER: `arr++` or `arr = ptr;` is an ILLEGAL compiler error!
- Pointer is a MUTABLE VARIABLE: `ptr++` is completely legal and advances the address.
NOTEBOOK PAGE 2 OF 3

2. Array of Pointers vs Pointer to an Array

Array of Pointers (`int *arr[5]`):

An array of 5 elements where each individual element is a pointer to an integer. Common for storing arrays of variable-length strings (ragged arrays).

Pointer to an Array (`int (*arr)[5]`):

A single pointer pointing to an entire array of 5 integers. The parentheses are mandatory due to operator precedence.

NOTEBOOK PAGE 3 OF 3

3. Function Pointers & Callback Mechanisms

Functions reside in the Code (Text) Segment of memory and have executable addresses!

Function Pointer Syntax: `return_type (*func_ptr)(param_types);`

Example: `int (*operation)(int, int);`

Assigning Address: `operation = add;`
Invoking via Pointer: `result = operation(10, 20);`
Application: Implements runtime dynamic dispatch, plugin architectures, and callbacks (e.g. `qsort()` comparator in `<stdlib.h>`).
Comparative Analysis: Array Name vs Pointer Variable
ParameterArray Name (e.g., int arr[5])Pointer Variable (e.g., int *ptr)
MutabilityConstant Pointer: Address cannot be changedMutable Variable: Can point to any memory location
Arithmetic Operationsarr++ or arr-- is an ILLEGAL compilation errorptr++ or ptr-- is completely legal
Memory AllocationReserves space for both array elements and structureAllocates only 4 or 8 bytes to store an address
sizeof OperatorReturns total bytes of entire array: 5 * 4 = 20BReturns pointer address size: strictly 4B or 8B
Function PassingDecays automatically to pointer to first elementPassed directly as address value

Interactive Tested Code Example

#include <stdio.h>

// Passing array via pointer syntax
void print_array(const int *arr, int size) {
    printf("Traversing array using pure pointer arithmetic: ");
    for (int i = 0; i < size; i++) {
        printf("%d ", *(arr + i)); // *(arr + i) == arr[i]
    }
    printf("\n");
}

// Function Pointer Callback
int add(int a, int b) { return a + b; }
int multiply(int a, int b) { return a * b; }

int main(void) {
    int data[4] = {10, 25, 50, 100};
    print_array(data, 4);

    // Function Pointer Demonstration
    int (*calc_ptr)(int, int);
    
    calc_ptr = add;
    printf("Function Pointer (add):      15 + 5 = %d\n", calc_ptr(15, 5));
    
    calc_ptr = multiply;
    printf("Function Pointer (multiply): 15 * 5 = %d\n", calc_ptr(15, 5));
    return 0;
}
💡 Note:Demonstrates array traversal via pointer offset *(arr + i) and runtime function pointer assignment and dispatch.

🎯 University Exam Scoring Blueprint

  • Remember the equivalence: arr[i] == *(arr + i) == *(i + arr) == i[arr].
  • Point out that sizeof(arr) gives total array byte size, but inside a function where arr decayed to a pointer, sizeof(arr) gives pointer size (4 or 8 bytes)!
  • Parentheses in int (*ptr)[5] are mandatory; without them, int *ptr[5] becomes an array of 5 pointers.

Top Viva Questions on Pointers with Arrays, Pointer Decay & Function Pointers

2 Questions
1

Why does sizeof(arr) behave differently inside main() versus inside a function receiving arr?

2

What is a Function Pointer?