Why Pointers Matter: A Visual Journey from Memory Cells to C Code
This article walks through a visual memory model, introduces a simple mov instruction, expands it with size‑specific variants, explains variables and their types, and then builds up the concept of pointers, their arithmetic, and how they map to actual CPU instructions.
1. Type System
Memory is visualized as a grid of byte‑sized cells, each with a decimal address. To store a value we define a simple instruction:
mov $29, (3)
mov $38, (6)Because a single cell holds only 0‑255, larger numbers need multiple cells. We introduce size‑specific mnemonics:
movb $29, (3) // 1 byte
movw $999, (8) // 2 bytes
movl $2147483647, (10) // 4 bytes
movq $..., (14) // 8 bytesThese correspond to the C types char, short, int, and long.
2. Variables
Instead of remembering raw cell numbers, we label cells with identifiers (variables). A variable’s declaration gives it a name and a type, which determines how many cells it occupies.
char a = 29;
char b = 38;
short c = 999;
char age = 18;
int salary = 2147483647;In C, a declaration ( int a;) tells the compiler to reserve space, while an assignment ( a = 1;) puts a value into that space.
3. Pointers
A pointer variable stores the address of another variable. Using & we obtain an address, and using * we dereference it.
short a = 1234; // assume a is at cell 6
int p = &a; // p holds the address 6On a 32‑bit system a pointer occupies 4 bytes regardless of the pointed‑to type.
short *p = &a; // p is a pointer to a shortThe * operator reads or writes the value at the stored address:
*p = 999; // changes the content of cell 6 (and 7)4. Pointer Arithmetic
Adding an integer to a pointer moves it by the size of the pointed‑to type. For an int *, p + 1 advances by 4 bytes.
If byte‑wise arithmetic is needed, cast the pointer to char *, add, then cast back:
p = (int *)((char *)p + 1);5. Essence of Pointers
From the CPU’s perspective a pointer is just a numeric address; the type information exists only for the compiler to generate correct load/store instructions. The operations &a, *p, and pointer arithmetic translate to simple assembly such as lea and mov.
6. Summary of Core Operations
Define a pointer: int *p; Initialize a pointer: p = &a; Dereference to modify: *p = 999; Pointer arithmetic: p = p + 1; Understanding these fundamentals clarifies why pointers exist, how they differ from ordinary variables, and how they are compiled into actual CPU instructions.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
