NBC 103 • Unit 314 min read★ High Exam Frequency
Pointers, Call by Reference, 2D Arrays & Strings
Unit 3: Arrays, Pointers & String Manipulation • Problem Solving Using C
👨🏫 Professor's Mental Model: The House Address Analogy
Variable tumhara ghar hai jisme data (jaise 42) rehta hai. Pointer ek paper slip hai jisme us ghar ka address (jaise 0x1000) likha hai. Address-of operator (&) address pata karta hai, aur Dereference operator (*) us address par jakar andar ka saman nikalta hai!
Pointer Operators (& and *) & Memory Addressing
Pointer variable stores the hexadecimal memory address of another variable. Syntax: int *ptr = &x;
- & (Address-of): Returns address of variable in RAM.
- * (Dereferencing): Accesses value stored at the target address.
Pointer Arithmetic: Incrementing (ptr++) advances the address by sizeof(*ptr) bytes (e.g. +4 bytes for int).
Call by Reference (Simulation via Pointers)
Passing memory addresses (&a, &b) allows a function to modify variables in the caller's stack frame directly, overcoming C's single return value limitation.
Strings in C: Null-Terminated Character Arrays
A string is a 1D character array ending with '\0'. Important <string.h> functions: strlen(), strcpy(), strcat(), strcmp(), strrev().
Pointer Memory Architecture Diagram
Pointer: ptr (int *)
Stores Address0x1000
At RAM: 0x2000Dereference *ptr
Variable: num (int)
Holds Value42
At RAM: 0x1000💡 Professor Key Point: Memory address
0x1000 is passed to functions in Call by Reference.Interactive Tested Code Example
#include <stdio.h>
void swap(int *x, int *y) {
int temp = *x;
*x = *y;
*y = temp;
}
int main(void) {
int a = 10, b = 25;
printf("Before Swapping: a = %d, b = %d\n", a, b);
swap(&a, &b); // Passing memory addresses
printf("After Swapping: a = %d, b = %d\n", a, b);
return 0;
}💡 Note:Demonstrates Call by Reference: swap() dereferences pointers *x and *y to exchange the original variables stored in main().
🎯 University Exam Scoring Blueprint
- Draw memory blocks showing addresses (e.g. 0x1000) whenever explaining pointers.
- Explain that array name (arr) acts as a constant pointer to its first element (&arr[0]).
- State that uninitialized pointers point to random memory and are called Wild Pointers.
Top Viva Questions on Pointers, Call by Reference, 2D Arrays & Strings
1