C Workshop · Print an Address
Lesson C

Print via a Pointer

Declare int x = 10 and print the value using a pointer, not x itself.

What it is

This lesson prints 10 by following a pointer, not by naming x in printf. You still create x, you point p at it, then you read *p. The value is 10; the address bits themselves stay hidden.

Approach

  1. int x = 10;
  2. int *p = &x;
  3. printf("%d\n", *p);

Where this is used in the real world

Diagram

1

x

10

2

p

&x

3

*p

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. This program prints:

Value via *p.

2. int *p = &x; makes p:

Stores address.

3. Printing p with %p would show:

Address bits.

4. If p were NULL, *p would be:

Do not deref NULL.

5. Another kind: p[0] when p points at x is:

Index 0.