f(x)
Pointers — Part 1
🏠 Home Lecture 18: Pointers Intro
Lecture 18 · The Core Concept

What exactly is a Pointer?

A Pointer is simply a variable that stores the memory address of another variable.

If a delivery driver needs to bring you a package, what must you give them?
📦

Regular Variable

Stores the actual data (e.g. 10, 'A').

📍

Pointer Variable

Stores a memory address (e.g. 0x104A).

Pointers Syntax

Declaring Pointers & The '&' Operator

To work with pointers, you place an asterisk (*) between the data type and the name, and use the & operator to get an address.

syntax.cAddress-of Operator
// 1. Declare a normal variable int score = 100; // 2. Declare a pointer to an integer int *ptr; // 3. Store the address of 'score' into 'ptr' ptr = &score; printf("Score is: %d\\n", score); printf("Address is: %p\\n", ptr);

🔍 The Address-of Operator (&)

The ampersand & is a unary operator that returns the memory address of its operand. Hover over the highlighted code on the left!
⚠️ Data Types Must Match: An int * can only point to an int. A char * can only point to a char.
Accessing Data

The Dereference (*) Operator

Once a pointer has an address, we can use the asterisk (*) to access or modify the actual value stored at that address.

dereference.cChanging Values
int a = 50; int *p = &a; // Reading the value printf("Value: %d\\n", *p); // Prints 50 // Modifying the value using pointer *p = 100; printf("New value: %d\\n", a); // Prints 100!

🔍 Inspect Dereferencing

Hover over the highlighted code on the left to see how the pointer interacts with memory!

⭐ The Two Faces of the Asterisk

Don't get confused! The asterisk does two different things depending on context:
1. Declaration
int *p;
"I am creating a pointer variable."
2. Dereference
*p = 10;
"Go to the address inside p and change the data."
89TH MINUTE SUBSTITUTION ⏱️

The "Manager's Pointer" in Action

The game is on the line. The pitch coordinate (Address 0x7FFE) is fixed on the grass. You are the Manager! Use your Pointer to substitute a new player into the Right Winger zone.

📋 Tactical Address POINTER
int *RW_Position = &rw_jersey;

This pointer directs us to the Fixed Coordinates on the pitch:

Coords: 0x7FFE
Execute Substitution:
Right Winger Pitch Zone
11

Salah

Value of rw_jersey: 11
FIXED COORDS: 0x7FFE
🏆 The Golden Rule: The pitch coordinate (0x7FFE) is fixed permanently on the grass. By using a Pointer (*RW_Position), the manager can remotely target that exact spot and replace the Player inside it (Data) with a new one!
Safety Rules

Null Pointers & Seg-Faults

What happens if you create a pointer but don't give it an address?

danger.cSegmentation Fault
int *ptr; // DANGER: Points to garbage memory! *ptr = 100; // 💥 CRASH: Segmentation Fault! /* --------------------------------- */ int *safe_ptr = NULL; // SAFE: Points to 0 if (safe_ptr != NULL) { *safe_ptr = 100; // Never executes, program safe! }

⚠️ The Garbage Address Trap

In C, uninitialized pointers point to random garbage addresses. Modifying those addresses might overwrite critical system memory, causing an instant crash (Seg-Fault).

Always initialize pointers to NULL (a macro for memory address 0) if you don't have a valid address yet.
Knowledge Check

Test Your Understanding

Question 1 of 5
Loading question...