C Workshop · Pointers
Foundation C

Pointers

A pointer holds an address. &x is the address of x. *p is the value at that address.

What it is

A pointer is a variable that holds an address — the location of another variable. &x is “where x lives.” *p is “open that address and read (or write) the value.”

Approach

  1. Declare a variable x.
  2. Point p at x with &x.
  3. Read or write through *p.

Where this is used in the real world

Diagram

1

x=10

int box

2

p=&x

arrow

3

*p

10

4

printf

10

Run algorithm

Each step highlights the matching C line. Program output appears only when a line that prints actually runs.

Ready
C code
1#include <stdio.h>
2int main(void) {
3    int x = 10;
4    int *p = &x;
5    printf("%d\n", *p);
6    return 0;
7}
What this line does
Press Run algorithm. The highlight will move through the C code.
Program output
(no output yet)

Check your understanding

Five questions. Pick one answer each, then Check answers. The correct choice turns green.

1. &x means:

Address-of.

2. *p when p = &x is:

Dereference.

3. Changing *p:

Same box.

4. A pointer that points nowhere should be:

NULL.

5. int *p declares:

Pointer to int.