Pointer Fundamentals

Back

Loading concept...

🏠 Pointers: Your Map to Computer Memory

Imagine your computer’s memory is a giant street with thousands of houses. Each house has a unique address number, and inside each house lives some data. A pointer is simply a piece of paper where you write down a house’s address.

That’s it! A pointer doesn’t hold the treasure—it holds the directions to find the treasure.


📍 Pointers Overview

The Big Picture

Think of your computer’s memory like a row of numbered mailboxes at the post office:

Mailbox 100: [5]
Mailbox 104: [42]
Mailbox 108: [17]

When you create a variable like int age = 25;, the computer picks a mailbox (let’s say mailbox 200) and puts 25 inside it.

A pointer is just another variable, but instead of storing a number like 25, it stores a mailbox number (address) like 200.

Why Do We Need Pointers?

  1. Share without copying — Instead of photocopying a whole book, just share its library location
  2. Change the original — A friend can update your actual notebook if they know where it is
  3. Work with big data — Moving a house address is easier than moving the house itself!

🏷️ Pointer Declaration

How to Create a Pointer

To create a pointer, you use the asterisk * symbol. Think of * as saying “this variable will hold an address.”

int *ptr;    // ptr can hold address
             // of an int variable

Breaking it down:

  • int = “I’ll point to an integer”
  • * = “I’m a pointer, not a regular variable”
  • ptr = name of our pointer

The Pattern

datatype *pointer_name;

Examples:

int *scorePtr;      // points to int
char *letterPtr;    // points to char
float *pricePtr;    // points to float

Memory Picture

Regular variable:     Pointer variable:
┌─────────┐           ┌─────────┐
│   25    │           │  1000   │
└─────────┘           └─────────┘
  age                   ptr
(holds value)         (holds address)

🔑 Address and Dereference

Two Magic Symbols

Symbol Name What it does
& Address-of “Tell me where you live”
* Dereference “Show me what’s inside”

Getting an Address with &

int treasure = 100;
int *map = &treasure;
// map now holds treasure's
// address!

Think of & as asking: “What’s your house number?”

Reading the Value with *

int treasure = 100;
int *map = &treasure;

printf("%d", *map);
// Prints: 100

The * says: “Go to this address and tell me what’s inside!”

Visual Flow

graph TD A["int treasure = 100"] --> B["Address: 2000"] C["int *map = &treasure"] --> D["map stores: 2000"] E["*map"] --> F["Go to 2000, find: 100"]

Changing Values Through Pointers

int score = 50;
int *ptr = &score;

*ptr = 99;  // Change what's
            // at the address

printf("%d", score);
// Prints: 99

You didn’t touch score directly, but it changed! The pointer was your remote control.


⚠️ Null Pointer

The “Nobody Home” Pointer

Sometimes a pointer doesn’t point anywhere yet. We use NULL to show “this pointer is empty.”

int *ptr = NULL;
// ptr points to nothing

Why Use NULL?

It’s like writing “TBD” (To Be Determined) on a form. It clearly shows:

  • “I haven’t assigned an address yet”
  • “This pointer is intentionally empty”

Always Check Before Using!

int *ptr = NULL;

if (ptr != NULL) {
    printf("%d", *ptr);
} else {
    printf("Pointer is empty!");
}

Rule: Never try to read from a NULL pointer—it’s like trying to visit a house that doesn’t exist!

Safety Pattern

// SAFE: Check first
if (ptr != NULL) {
    *ptr = 10;  // Only do this
                // if valid
}

// DANGEROUS: No check
*ptr = 10;  // CRASH if NULL!

📦 Pointers and Arrays

The Secret Connection

Here’s a mind-blowing fact: Array names ARE pointers!

int nums[3] = {10, 20, 30};

// These are the SAME:
printf("%d", nums[0]);
printf("%d", *nums);
// Both print: 10

How It Works

Array in memory:
Address:  100   104   108
        ┌─────┬─────┬─────┐
nums →  │ 10  │ 20  │ 30  │
        └─────┴─────┴─────┘
Index:    [0]   [1]   [2]

The name nums is actually the address 100!

Pointer Arithmetic

When you add to a pointer, it jumps to the next element:

int nums[3] = {10, 20, 30};
int *ptr = nums;

printf("%d", *ptr);       // 10
printf("%d", *(ptr + 1)); // 20
printf("%d", *(ptr + 2)); // 30

Translation Table:

Array Style Pointer Style Value
nums[0] *ptr 10
nums[1] *(ptr+1) 20
nums[2] *(ptr+2) 30

Walking Through an Array

int nums[4] = {5, 10, 15, 20};
int *ptr = nums;

for (int i = 0; i < 4; i++) {
    printf("%d ", *(ptr + i));
}
// Output: 5 10 15 20

📝 Pointers and Strings

Strings ARE Character Arrays

In C, a string is just an array of characters ending with \0:

char word[] = "Hi";
// Same as: {'H','i','\0'}

String Pointers

char *greeting = "Hello";

This creates a pointer to the first character 'H'.

Memory:
┌───┬───┬───┬───┬───┬────┐
│ H │ e │ l │ l │ o │ \0 │
└───┴───┴───┴───┴───┴────┘
  ↑
greeting points here

Walking Through a String

char *msg = "Cat";
char *ptr = msg;

while (*ptr != '\0') {
    printf("%c ", *ptr);
    ptr++;
}
// Output: C a t

Two Ways to Declare Strings

// Method 1: Array (can modify)
char name[] = "Sam";
name[0] = 'J';  // OK! Now "Jam"

// Method 2: Pointer (read-only!)
char *name2 = "Sam";
// name2[0] = 'J'; // DANGER!

Remember: String literals with pointers are stored in read-only memory!


🎁 Pass by Pointer

The Problem with Regular Functions

void addTen(int num) {
    num = num + 10;
}

int main() {
    int score = 5;
    addTen(score);
    printf("%d", score);
    // Still prints: 5 😢
}

The function got a copy, not the original!

The Solution: Pass the Address

void addTen(int *numPtr) {
    *numPtr = *numPtr + 10;
}

int main() {
    int score = 5;
    addTen(&score);
    printf("%d", score);
    // Prints: 15 🎉
}

Visual Explanation

graph TD A["main: score = 5"] -->|"Pass &score"| B["addTen receives address"] B --> C["*numPtr = *numPtr + 10"] C --> D["Changes original score!"] D --> E["main: score = 15"]

Classic Example: Swap Function

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main() {
    int x = 10, y = 20;
    swap(&x, &y);
    printf("x=%d y=%d", x, y);
    // Output: x=20 y=10
}

Why Pass by Pointer?

Without Pointer With Pointer
Function gets a copy Function gets the address
Changes stay inside function Changes affect original
Safe but limited Powerful but needs care

🎯 Quick Reference

Symbol Summary

Symbol In Declaration In Expression
* “I’m a pointer” “Go to address, get value”
& “Give me the address”

Common Patterns

// Declare pointer
int *ptr;

// Assign address
ptr = &variable;

// Read value at address
int val = *ptr;

// Change value at address
*ptr = newValue;

// Check for null
if (ptr != NULL) { ... }

🌟 You Did It!

You now understand:

  • ✅ What pointers are (address holders)
  • ✅ How to declare them (int *ptr)
  • ✅ Address (&) and dereference (*)
  • ✅ NULL pointers (safety first!)
  • ✅ Arrays and pointers (they’re connected!)
  • ✅ String pointers (character arrays)
  • ✅ Pass by pointer (modify originals)

Pointers might seem tricky at first, but they’re just addresses. Once you see memory as a street of houses, everything clicks! 🏠🔑

Loading story...

Story - Premium Content

Please sign in to view this story and start learning.

Upgrade to Premium to unlock full access to all stories.

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.