NBC 103 • Unit 316 min readHigh Exam Frequency

Pointer Fundamentals, Memory Addressing & Address Arithmetic

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

👨‍🏫 Professor's Mental Model: The Physical House vs Street Address Note

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 (&) ghar ka address pata karta hai, aur Dereference operator (*) us address par jakar ghar ka darwaza kholta hai aur andar ka saman nikalta hai! Pointer arithmetic sadak par chalne jaisa hai: agar int pointer ko +1 karoge toh wo 1 byte nahi balki pure 4 bytes aage koodega!

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. Computer Memory Architecture & Pointer Declaration

Memory as Byte Grid: Every byte in computer RAM has a unique hexadecimal address (e.g., `0x7ffee4b28abc`).
Pointer Definition: A variable that stores the memory address of another variable rather than a direct value.
Syntax: `data_type *ptr_name;` (e.g. `int *ptr;` means ptr points to an integer).
The Two Essential Operators:

1. Address-of Operator (`&`): Returns the physical memory location of a variable.

2. Dereferencing / Indirection Operator (`*`): Directly accesses or mutates the value stored at the target address pointed to by the pointer.

NOTEBOOK PAGE 2 OF 3

2. The 4 Special Pointer Types

NULL Pointer: A pointer explicitly assigned to address 0 (`ptr = NULL;`). Indicates that the pointer does not reference any valid object. Dereferencing NULL causes a crash.
Void Pointer (Generic Pointer - `void *`): Can hold the memory address of ANY data type. It CANNOT be directly dereferenced; it must first be typecast to a concrete pointer type: `*(int *)vptr`.
Wild Pointer: An uninitialized pointer pointing to an arbitrary, unpredictable memory location. Highly dangerous!
Dangling Pointer: Points to a memory address that has already been deallocated or freed. Accessing it triggers undefined behavior.
NOTEBOOK PAGE 3 OF 3

3. Pointer Arithmetic Rules & Scaling

Pointer arithmetic is strictly governed by the data type size (`sizeof(*ptr)`):

Increment (`ptr++`): Moves pointer forward by `sizeof(type)` bytes.

Example: If `int *p = 1000`, then `p++` results in address `1004` (not 1001!).

Decrement (`ptr--`): Moves backward by `sizeof(type)` bytes.
Difference (`ptr1 - ptr2`): Valid only when both pointers reference elements of the SAME array. Returns the number of array elements between them.
Illegal Operations: Adding two pointers (`p1 + p2`), multiplying, or dividing pointers is ILLEGAL and produces compilation errors!

RAM Pointer Architecture: int num = 42; int *ptr = #

Pointer: ptr (int *)
Stores Address0x1000
At RAM: 0x2000
Dereference *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>

int main(void) {
    int num = 42;
    int *ptr = &num;

    printf("=== POINTER MEMORY DEREFERENCE DEMO ===\n");
    printf("Value of 'num':              %d\n", num);
    printf("Address of 'num' (&num):     %p\n", (void *)&num);
    printf("Value in pointer 'ptr':      %p\n", (void *)ptr);
    printf("Address of pointer (&ptr):   %p\n", (void *)&ptr);
    printf("Dereferenced value (*ptr):   %d\n\n", *ptr);

    // Mutating variable through pointer indirection
    *ptr = 99;
    printf("After *ptr = 99 -> num value is now: %d\n\n", num);

    // Pointer Arithmetic Scaling Demo
    int arr[3] = {10, 20, 30};
    int *p_arr = arr;
    printf("Base Address (p_arr):         %p\n", (void *)p_arr);
    printf("p_arr + 1 (Advances 4 bytes): %p\n", (void *)(p_arr + 1));
    return 0;
}
💡 Note:Demonstrates memory address inspection, pointer dereferencing to mutate target variable in-place, and address scaling by 4 bytes during pointer arithmetic.

🎯 University Exam Scoring Blueprint

  • Always draw the memory box diagram with addresses (e.g. 0x1000 and 0x2000) when explaining pointers in exams.
  • Explicitly state that adding two pointers together (p1 + p2) is a compilation syntax error.
  • Emphasize that void * is a generic pointer requiring explicit typecasting before dereferencing.

Top Viva Questions on Pointer Fundamentals, Memory Addressing & Address Arithmetic

2 Questions
1

What is the size of a pointer variable in C?

2

What is a Dangling Pointer and how do you remediate it?