Unlocking Pointers: From Memory Cells to Real C Code
This article walks through the fundamentals of memory layout, type systems, variable definition, and pointer operations in C, illustrating how simple mov instructions map to variable storage, how pointers are declared and used, and why pointer arithmetic behaves the way it does.
Type System
Memory is visualized as a series of byte-sized cells indexed by decimal addresses. Simple mov $value, (address) instructions place numbers into cells, e.g.
mov $29, (3)
mov $38, (6). To express the size of data, suffixes are introduced: movb for 1‑byte, movw for 2‑byte, movl for 4‑byte, and movq for 8‑byte values.
Variables
Instead of remembering raw addresses, labels (variable names) are attached to cells. Each label carries both a name and its size, mirroring C type declarations such as
char a = 29;
char b = 38;
short c = 999;
char age = 18;
int salary = 2147483647;. This bridges the assembly‑style mov syntax with high‑level C syntax.
Variable Definition and Assignment
Defining a variable and assigning a value are distinct steps. For example, int a = 1; can be split into
// definition
int a;
// initialization
a = 1;. The definition informs the compiler, while the assignment generates the actual memory‑store instruction.
Pointers
A pointer variable stores a memory address. Declaring
short a = 1234; // placed at cell 6
int p = &a; // p holds address 6makes p a pointer to a. The pointer’s own size is fixed (e.g., 4 bytes on a 32‑bit system), independent of the pointed‑to type.
To indicate the type of the pointed‑to data, the pointer declaration includes the base type, e.g., short *p = &a;, meaning p points to a short value.
Pointer Dereferencing and Assignment
The dereference operator * accesses the value at the address stored in a pointer. Changing the pointed value is done with *p = 999;, which translates to an assembly store like movl $999, (6).
Pointer Arithmetic
Adding an integer to a pointer advances it by the size of the pointed‑to type. For an int *p, p + 1 moves the address forward by 4 bytes, not by a raw numeric increment. If raw arithmetic is needed, the pointer can be cast to char *, incremented, then cast back, e.g., p = (int *)((char *)p + 1);.
Pointer Essence
From the CPU’s perspective, pointers are just numbers (addresses) stored in memory cells; the type information exists only for the compiler to generate correct load/store instructions. The article emphasizes that pointers are tools for programmers, not mystical constructs.
Conclusion
The piece summarizes the key steps: declare a pointer, initialize it with an address, modify the pointed value with *p, and perform pointer arithmetic. Understanding these operations clarifies how high‑level C code maps to low‑level memory actions.
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.
