Skip to content

Addresses and Pointers

The Course
•
32 min study

On Day 3 a function could not change its caller's int, because it only received a copy. On Day 4 a function could change its caller's array, because it received the location of the elements instead. That location has a name: an address. Every object in a running program lives somewhere in memory, and a pointer is a variable whose value is the address of another object. With a pointer you can reach an object without naming it, which is how a function updates its caller, how array parameters work, and how much of C's power and danger arises.

Predict and run

minutes is never assigned directly after its declaration. Predict what prints.

Predict before running C
#include <stdio.h>

int main(void) {
    int minutes = 20;
    int *target = &minutes;
    *target += 5;
    printf("%d\n", minutes);
    return 0;
}
Check your prediction
Text
25

&minutes produced the address of minutes, and target stored it. *target += 5 followed that address and changed the object found there, which is minutes. There was only ever one integer; target was simply another way to reach it.

Two objects, two meanings of a star

A pointer and the object it points to are separate objects with separate values. target holds an address. minutes holds 20, then 25. Keep them apart by reading the symbols carefully:

  • In a declaration, int *target means "target is a pointer to an int".
  • In an expression, *target means "the int that target points to". This is called dereferencing.
  • &minutes means "the address of minutes".

The same * has a different job in a declaration and in an expression. Changing *target changes the pointed-to object. Changing target itself makes it point somewhere else and leaves both objects alone.

Redirect the pointer, not the value C
#include <stdio.h>

int main(void) {
    int planned = 30;
    int done = 10;
    int *p = &planned;
    *p = 35;
    p = &done;
    *p = 15;
    printf("%d %d\n", planned, done);
    printf("%d\n", p == &done);
    return 0;
}
Text
35 15
1

The first store went to planned. Then p was redirected, and the second store went to done. Comparing p with &done asks whether they hold the same address, and the answer is 1. The examples predict values and comparisons, never printed addresses: the numbers themselves change from run to run and machine to machine, so they make poor expected output.

A pointer must point at something valid

Dereferencing is only allowed when the pointer holds the address of an object that currently exists. Two values break that rule.

An uninitialized pointer. int *p; with no value holds indeterminate bits. *p = 5; writes to an unpredictable address. It might crash, or it might silently overwrite another variable and cause a wrong answer much later. It is undefined behavior, and the compiler may warn that p is used uninitialized.

The null pointer. NULL, available from <stdio.h> and <stddef.h>, is a pointer value that deliberately points at nothing. It is the right value for "no target yet". It is safe to store and compare, but dereferencing it is undefined behavior. On most desktop systems it crashes:

Shell
Segmentation fault (core dumped)

In this browser, C runs as WebAssembly, where address 0 is ordinary readable memory, so dereferencing NULL here may print a value and carry on. That does not make it correct: the same code crashes on your own machine, and it is undefined behavior everywhere.

So initialize every pointer, either with a real address or with NULL, and check for NULL before dereferencing whenever it is a possibility.

Arrays and pointers

Yesterday's array parameters were pointers in disguise. In most expressions, the name of an array turns into the address of its first element. That is what a function receives, and values[i] means "the element i places after the one values points to".

An array name becomes an address C
#include <stdio.h>

int main(void) {
    int minutes[3] = {25, 10, 15};
    int *first = minutes;
    *first = 30;
    first[2] = 40;
    printf("%d %d %d\n", minutes[0], minutes[1], minutes[2]);
    printf("%d\n", first == &minutes[0]);
    return 0;
}
Text
30 10 40
1

Indexing through the pointer reached the same elements as indexing the array. A pointer into an array carries no length, so the same bounds rules apply: first[3] would be past the end.

Practice

Exercise

Your turn

Declare morning and evening, both 20. Use a single pointer, current, for every change: point it at morning and add 10, print both, then point it at evening and store 45, print both again, and finally print whether current points at evening.

Text
Morning: 30, evening: 20
Morning: 30, evening: 45
Points at evening: 1
Your workspace C
#include <stdio.h>

int main(void) {
    int morning = 20;
    int evening = 20;
    // Write your solution here.
    return 0;
}
Hint 1: the idea

Assigning to current chooses the target. Assigning to *current changes the target. Each change in the brief is one of each.

Hint 2: a first step

Write int *current = &morning; and then *current += 10;. Print both variables and check that only morning changed.

Show one solution
C
#include <stdio.h>

int main(void) {
    int morning = 20;
    int evening = 20;
    int *current = &morning;
    *current += 10;
    printf("Morning: %d, evening: %d\n", morning, evening);
    current = &evening;
    *current = 45;
    printf("Morning: %d, evening: %d\n", morning, evening);
    printf("Points at evening: %d\n", current == &evening);
    return 0;
}
Text
Morning: 30, evening: 20
Morning: 30, evening: 45
Points at evening: 1

morning and evening were never assigned by name after their declarations, yet both changed, because current led to each in turn. Every dereference happened while current held the address of a live variable. Next you will pass a pointer into a function so that it can update a variable belonging to its caller.

Was this article helpful?

Your response is saved on this device.