LearnCodeVisuallyBack to lesson
NEW · INTERACTIVE 3D LAB

3D Memory & References Lab

See how variables connect to objects. Change the values and follow the code one step at a time.

Understand the connection before moving the diagram

A C pointer can store the address of an object. The pointer and the object are different things: changing the pointed-to object does not change the address stored in the pointer. This lab uses symbolic cells, not actual machine addresses. Choose C to explore this example; other language modes explain their own reference rules.

Worked C example: update an integer through a pointer
#include <stdio.h>
int main(void) {
    int score = 10;
    int *p = &score;
    *p = 20;
    printf("%d %d\n", score, *p);
    return 0;
}
  1. score starts at 10.
  2. &score supplies its address, stored in p.
  3. *p = 20 writes to the existing integer.
  4. Both reads produce 20, so the output is 20 20.

Save as pointer.c, compile with gcc -std=c11 pointer.c -o pointer, then run ./pointer. The local integer remains alive throughout this example. A null pointer or a pointer to an object whose lifetime has ended must not be dereferenced.

Try it: insert int other = 30; p = &other; before printing. Predict the result.

Show the answer

It prints 20 30. Reassigning p changes which object it points to; it does not undo the earlier update to score. Both local objects are still alive.

In the interactive lab below, change the initial and mutation values, press Apply, and follow the corresponding source. The program is modeled rather than executed in the browser. Read the complete C pointer lesson or browse all visual labs. This reading is in English; the lab controls and explanations offer ten languages.

Memory explorer

Symbolic objects and connections, not physical addresses or a memory layout.

Live objectLifetime ended
Read memory state as text

Source code ·

GUIDED TRACE

The highlighted instruction matches the current step.

Program output

GUIDED TRACE

Flat view