Pointers with Arrays, Pointer Decay & Function Pointers
Unit 3: Arrays, Pointers & String Manipulation • Problem 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
1. The Equivalence Principle: Arrays and Pointers
In C, arrays and pointers are deeply intertwined through the Equivalence Principle:
`arr[i]` is identical to `*(arr + i)`
`i[arr]` is also syntactically legal in C because addition is commutative: `*(i + arr)`!
2. Array of Pointers vs Pointer to an Array
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).
A single pointer pointing to an entire array of 5 integers. The parentheses are mandatory due to operator precedence.
3. Function Pointers & Callback Mechanisms
Functions reside in the Code (Text) Segment of memory and have executable addresses!
Example: `int (*operation)(int, int);`
| Parameter | Array Name (e.g., int arr[5]) | Pointer Variable (e.g., int *ptr) |
|---|---|---|
| Mutability | Constant Pointer: Address cannot be changed | Mutable Variable: Can point to any memory location |
| Arithmetic Operations | arr++ or arr-- is an ILLEGAL compilation error | ptr++ or ptr-- is completely legal |
| Memory Allocation | Reserves space for both array elements and structure | Allocates only 4 or 8 bytes to store an address |
| sizeof Operator | Returns total bytes of entire array: 5 * 4 = 20B | Returns pointer address size: strictly 4B or 8B |
| Function Passing | Decays automatically to pointer to first element | Passed 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;
}🎯 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.