CS210 · Block 2 · Lesson 12

Pointers Introduction

Addresses · The & operator · Pointer declarations · Dereferencing · NULL
At a glance: The hardest and newest topic in CS210. Every variable lives at an address in memory, and a pointer is a variable that holds one of those addresses. Get the picture of "address vs value" locked in today and the rest of the block (arithmetic, pass by pointer, malloc) follows naturally. The pointer walkthrough in Section 5 shows you addresses and arrows as you type; the lab makes you write the syntax yourself.
Contents
  1. Lesson Objectives
  2. Assigned Readings
  3. Pre-Class Work
  4. Lesson Materials and Overview
  5. In-Class Work
  6. After Class
1. Lesson Objectives

By the end of this lesson, cadets will be able to:

  1. Distinguish between a variable’s value and its memory address. (Outcome 2)
  2. Declare pointer variables and assign them using the address-of operator. (Outcome 2)
  3. Access the value pointed to by a pointer using the dereference operator. (Outcome 2)
  4. Recognize the role of NULL pointers and check for them appropriately. (Outcomes 2, 7)
2. Assigned Readings

Complete before class. This is the hardest reading of the course so far. Plan extra time and read slowly.

Source Sections Why
Beej-C Chapter 5 (Pointers — Cower In Fear!) All seven subsections. Read every one.
King, C Programming: A Modern Approach Chapter 11 (Pointers) Optional alternate explanation if Beej is not clicking
3. Pre-Class Work
Do this first
Plan on 45 to 60 minutes. This is the hardest reading of the course so far. Do not skim. If something is not clicking, mark the spot and bring a specific question to class.

Pre-Class Checklist

1
Read Beej-C Chapter 5 (Pointers — Cower In Fear!)
All seven subsections. Read every one carefully. Make sure you can answer these checkpoint questions in your own words before class:
  • What is the difference between a variable’s value and its address?
  • What does the & operator do?
  • What does the * operator do in a declaration vs in an expression?
  • Why is dereferencing a NULL pointer dangerous?
  • In int *p, q;, what are the types of p and q?
2
Try the & example from the reading
Type Beej’s first & example into a new file and run it. See an address come out on your machine.
// pointer_check.c
#include <stdio.h>

int main(void) {
  int i = 42;
  printf("value of i: %d\n", i);
  printf("address of i: %p\n", (void *)&i);
  return 0;
}
The address will be a long hex number that changes between runs. That is normal. The point is to see your own variable’s address with your own eyes.
3
Skim the In-Class section below
You will see a pointer walkthrough widget. No need to use it yet; just know what it looks like. Each row shows a variable’s address and value. Pointers draw an arrow to whatever they point at.
4. Lesson Materials and Overview

Pointers are the topic that separates “I’ve written some C” from “I can read any C codebase.” The mental model takes a beat to land. Once it does, every later topic in this block (pointer arithmetic, pass by pointer, malloc, structs with pointer fields) builds on the same picture. So we start slow and concrete: addresses, the & operator, pointer variables, dereferencing, and NULL. That’s it for today.

Jump to
Slides Mental Model Address-of Declaring Pointers Dereferencing NULL Gotchas

Slides

The Lesson 12 deck covers the mental model, the & operator, pointer declarations, the dereference operator, NULL, and the declaration gotcha. The slides set up the in-class lecture; the pointer walkthrough is where you practice each piece yourself.

Open Lesson 12 slides →

Mental model: memory is numbered boxes

Think of memory as a long row of numbered boxes, each holding one byte. The number on each box is its address. When you write int x = 42;, the compiler picks four consecutive boxes, writes the bits of 42 into them, and from then on the name x refers to those boxes. The address of the first box is the address of x.

A pointer is a variable whose value is an address. Same idea as a variable holding 42, except the number it holds is the location of some other variable. That’s it. Everything else in this lesson is syntax for asking “what is the address of this variable?” and “what is at the address this pointer holds?”

The address-of operator: &

The & operator, when placed in front of a variable, gives you the address of that variable. The result is a pointer.

int x = 42;
printf("%d\n", x);        // 42 (the value)
printf("%p\n", (void *)&x); // 0x7ffe... (the address)

Print pointer values with %p. Cast to (void *) to keep the compiler quiet. The exact number you see will be different on every run; that’s the operating system handing your program a different stretch of memory each time, and it is normal.

You can recognize the address-of operator in any expression: a single & in front of a variable name. (Double && is “logical and” from CS110, totally unrelated.)

If this looks familiar, it should. Every scanf call back in Lesson 3 used the & operator: scanf("%d", &x). We described it then as a required syntactic ritual without explaining why. Now you can see it: scanf needs to know WHERE to store the value you type, and &x gives it that location. The full payoff lands at Lesson 14, when we look at how any function can use a pointer parameter to modify its caller’s variables.

Declaring pointer variables

A pointer declaration looks like a regular variable declaration with a * inserted before the name. The type before the * is the type of the thing the pointer will point at.

int x = 42;
int *p;     // p is a pointer to an int (uninitialized!)
p = &x;    // p now holds the address of x

Read the declaration as “p is a pointer to an int.” The type matters: an int * can only point at ints. A char * can only point at chars. This becomes important when we get to pointer arithmetic in Lesson 13.

An uninitialized pointer is dangerous. It holds whatever garbage bits were in those bytes already. Treat that garbage as a random address and the program will crash (or worse, silently corrupt memory). Always assign a real address to a pointer before you use it. If you do not have one yet, assign NULL (see below).

The dereference operator: *

When you have a pointer, the * operator (used in an expression, not a declaration) gives you access to the thing the pointer points at. You can read it, write it, do arithmetic with it; it acts as an alias for the original variable.

int x = 42;
int *p = &x;

printf("%d\n", *p);  // 42 (read x through p)
*p = 100;
printf("%d\n", x);   // 100 (x changed, because *p IS x)

The two uses of * are the most confusing part of pointer syntax. In a declaration like int *p;, the * means “this variable is a pointer.” In an expression like *p = 100, the * means “follow the pointer and use the thing it points at.” Same character, different jobs. Context tells you which.

NULL: the “points at nothing” pointer

NULL is a special value that means “this pointer does not currently point at anything.” You can assign it to any pointer of any type. Use it to mark a pointer as “not ready yet” instead of leaving it uninitialized.

int *p = NULL// safe: explicitly not pointing anywhere

if (p != NULL) {
  printf("%d\n", *p); // only dereference if it's safe
}

Dereferencing a NULL pointer crashes your program. The operating system specifically watches for reads and writes to address zero and kills your process. This is actually good news: a crash you can find in seconds is better than a silent memory corruption you might hunt for hours.

The habit that prevents this: any pointer that might be NULL gets checked with if (p != NULL) before dereferencing. You will see this pattern constantly in C code from Lesson 15 onward.

Two declaration gotchas

The asterisk binds to the name, not the type. When declaring multiple variables on one line, the * only applies to the name immediately after it. The line below declares one pointer and one plain int, despite what your eyes might tell you.

int *p, q;  // p is int*, q is int. NOT both pointers.
int *p, *q; // Both pointers. Each one needs its own *.

The cs210 style rule: in CS210, declare one variable per line. It is more typing but it eliminates this whole class of bug.

Pointer types must match. An int * cannot point at a double. The compiler will warn you and the bytes will be misinterpreted if you ignore the warning.

double d = 3.14;
int *p = &d;  // warning: incompatible pointer types

When you compile with -Wall -Werror (which you are, per the cs210 Makefile), the compiler will refuse to build. Fix the types; don’t cast the warning away.

5. In-Class Work

Three things to do in class. Use the checklist to track your progress.

  • Take Quiz 4 (Recursion I). Covers Lesson 11 content. Your instructor will tell you when in the period to take it. About 15 minutes.
  • Work through the pointer walkthrough below. Twelve guided tasks across four sections. Watch addresses, arrows, and dereferencing in action. About 25 minutes.
  • Complete Lab 12: Pointer Basics. Autograded through GitHub Classroom. Full instructions in the lab repo.

Pointer Walkthrough

Twelve tasks across four sections. Type each C statement at the prompt. The terminal shows what the parser did; the right pane shows the current memory state with addresses and arrows from pointers to their targets.

Before you begin

Enter your name. It will appear on the completion screen so you can include it in your screenshot for the Blackboard reflection.

CS210 Lesson 12 · Pointer Walkthrough

A guided tour through addresses, the & operator, pointer declarations, dereferencing, and NULL. Type each C statement on the input line. The terminal shows what happened; the right pane shows memory with arrows from pointers to their targets.
Task 0 of 12
Section 1: Addresses and the & operator
Current task
Loading...
Memory
No variables yet.
Type a declaration on the left.
ptr>
How this works: This is not a real C compiler. It is a teaching tool that shows you addresses and pointer relationships you cannot see in normal terminal output. The task card tells you what to type next; only the expected command will advance the walkthrough so the memory state stays predictable. Mistype twice and the hint button will pulse. After the walkthrough completes, free mode unlocks automatically so you can experiment.
6. After Class
  • Finish Lab 12 if you did not complete it in class. Submit through GitHub Classroom before Lesson 13.
  • Submit the Lesson 12 reflection in Blackboard. You will need the completion code from the pointer walkthrough plus a screenshot of the completion screen.
  • Read Beej-C Chapter 11 (Pointers II: Arithmetic) for Lesson 13. We’ll extend today’s walkthrough with pointer arithmetic and the array-pointer connection.
  • Replay the walkthrough in free mode. Try declaring your own variables and pointing at them. See if you can predict the address before you press Enter.
  • Heads up: Quiz 5 at Lesson 14 covers today’s pointer introduction material.
Need help? Schedule EI with your instructor. Bring specific code or specific screenshots, and the exact error message or output you do not understand. Specific questions get unstuck quickly. For Git issues (push rejected, merge conflicts, recovering work), check the Git troubleshooting reference card in Blackboard under Resources before opening a help request.