Here’s the line that makes pointers click for me: **pp = 42;. There are three objects involved, two arrows to follow, and exactly one integer changes. Before running it, can you point to the box that gets the new value?
The drawings in Kenneth Reek’s Pointers on C helped me learn to answer questions like that.
Give the pointer a box of its own
int samples[] = {18, 25, 31, 28};
int *p = samples;
int **pp = &p;samples is an array containing four integers side by side. In the assignment to p, the array expression converts to a pointer to its first element. p is a separate object that stores that pointer. Because p itself is an object, we can take its address too: &p is the value we store in pp.
Read the first frame using three addresses. The integer 18 lives at 0x1000. The pointer object at 0x2000 holds 0x1000. The pointer object at 0x3000 holds 0x2000. An address under a box tells you where it lives; the number inside tells you what it holds. These addresses and four-byte integers belong to the example. Your compiler can choose different locations and sizes.
Advance the pointer. Then write through it.
On the first click, ++p moves from &samples[0] to &samples[1]. Its stored address becomes 0x1004 because one integer in this example occupies four bytes. The box for p is still at 0x2000. That’s why pp needs no update.
On the next click, read **pp one star at a time. *pp designates the pointer object p. Dereference that pointer and you reach samples[1]. Assign 42 there. Read back through samples[1], *p, or **pp and you see the same 42. Three expressions give you access to the same integer.
Now imagine we saved int *first = p; before incrementing p. After the increment, *first still reads 18. Copying a pointer copies its current value. Taking &p gives you a way to observe later changes to p itself. That’s the distinction I’d draw before memorising any double-pointer syntax.
Where does the walk stop?
p[i] designates the same element as *(p + i). C scales the step by the size of the pointed-to type. You can form samples + 4, one position past this array, and compare against it to stop a loop. Dereference only while you’re inside the array. The address one past the end can be meaningful even though there’s no fifth element to read.
Notice where the program below computes its count. In the scope where samples is the array, sizeof samples / sizeof samples[0] gives four. A function parameter written int samples[] is adjusted to a pointer parameter. Within that function, the same expression divides the size of a pointer by the size of an element. Pass the element count along with the pointer.
Read “hello” as six bytes
Choose Raw bytes and read 68 65 6C 6C 6F 00. In ASCII, the first five byte values spell hello. The sixth is the zero terminator. In char greeting[] = "hello";, sizeof greeting counts all six bytes; strlen(greeting) counts the five characters before the zero. The dot at the end of the hex dump’s text column is how the viewer displays a non-printable byte.
Try the 32-bit and 64-bit address settings. The addresses gain digits, but the string still occupies six bytes. Then select the three pointer slots. In these illustrative layouts they occupy 12 or 24 bytes respectively. The extra space belongs to the pointer objects. None of the three strings gets any longer.
Why names[1][1] takes two steps
const char *names[] = {"red", "blue", "tan"};Read const char *names[3] as “names is an array of three pointers to const char.” The 3 is a count in the declaration. As a subscript in an expression, names[3] would attempt to access a fourth slot. The valid indices are 0, 1 and 2.
In names + 1, the array converts to a pointer to its first slot: names == &names[0]. Dereferencing that base gives *names, the first slot, whose stored pointer leads to the letter r. A second dereference, **names, reads the character itself. This conversion does not allocate a hidden pointer in front of the array. Try sizeof names here: it measures all three slots.
The first index selects the second pointer slot. On the example 64-bit layout, the table begins at 0x4000, so that slot lives at 0x4008. Read its contents: 0x6000. That value takes us to the first byte of blue. The second index advances by one char, to 0x6001. The result is the letter l.
A small change to the declaration gives you a different layout: char names[3][5] = {"red", "blue", "tan"};. Now you have three adjacent five-character arrays. Selecting row 1 advances five bytes directly; there’s no pointer slot to load. The expression names[1][1] still reads the same letter. This is why I like seeing the declaration alongside the diagram.
Two brackets: where do a[0][1] and a[1][0] live?
In the opening example, **pp meant “read the address stored in a pointer, then follow it to an integer.” A two-dimensional array also lets me write two subscripts. What I want to know when I see a[i][j] is: does the first step read a stored address, or can the compiler calculate the row address from its width?
int a[2][3] = {{10, 11, 12},
{20, 21, 22}};Read the first bracket as “which row?” and the second as “how far into that row?” a[0][1] gives me 11, one integer into the first row. a[1][0] gives me 20, the first integer of the next row. Each row is itself an array of three integers. C lays those rows next to each other in order.
Give the first integer the example address 0x1000 and assume four-byte integers. The address of a[i][j] is the base byte address plus (i * 3 + j) * 4. So 11 lives at 0x1004 and 20 lives at 0x100C. The last integer of row zero is a[0][2]. To read the next one, use a[1][0]. Writing a[0][3] goes outside the first row even though the rows touch.
When would I want a pointer to a row? When would I want a pointer to a pointer?
For a grid with three columns in every row, I can pass a to a function expecting int (*row)[3]. In that call, a converts to a pointer to its first row. row + 1 steps over one whole three-integer row. For rows of different lengths, or rows I want to reorder, I can build a separate array of int * and keep track of each row’s length myself. That pointer table can be passed to a function expecting int **. Casting the original grid to int ** cannot create those pointer slots.
Try reversing the rows without copying any integers. Predict p[0][1] before running this. The first pointer now leads to the row beginning with 20, so moving one integer into it gives 21.
#include <assert.h>
int main(void)
{
int a[2][3] = {{10, 11, 12}, {20, 21, 22}};
int (*row)[3] = a; // points to a three-int row
int *rows[2] = {a[1], a[0]}; // store the row addresses in reverse
int **p = rows; // points to the first pointer slot
assert(sizeof a == 6 * sizeof(int));
assert(row + 1 == &a[1]);
assert(a[0][1] == 11 && a[1][0] == 20);
assert(p[0][1] == 21 && p[1][0] == 10);
assert(&p[0][1] == &a[1][1]); // two names for the same integer
}Where do these boxes live?
With this declaration inside a function and no static, the pointer array has automatic storage duration. A typical implementation puts its slots in a stack frame if it needs to materialize them. The string literals have static storage duration and are often placed in a read-only data section. Returning from the function ends the local array’s lifetime; the literals still exist. This example makes no heap allocation. The arrows describe which objects are connected, independently of where a compiler places them.
One process, two thread stacks, one allocated byte
Suppose a helper declares unsigned char *p = NULL;, asks malloc(1) for one byte, checks for failure, writes 42, and returns the pointer. There are two boxes to keep track of: the local pointer and the allocation it names. Step to Return pointer. The helper’s local box disappears. The caller still has the pointer value and can read 42 until it calls free. Had the helper returned the address of its own local byte, the byte’s lifetime would already have ended.
Where does the DIMM enter when I read *p?

Picture a helper with a local pointer p and one allocated byte containing 42. Suppose the compiler has put p in its stack frame. Reading that slot gives the virtual address 0x10000040. Reading *p uses that address to reach the byte on the heap. The CPU may already have p in a register, in which case it can skip the load of the stack slot.
Who translates the address? Linux maintains page tables for the process. The CPU’s memory management unit (MMU) uses them. For this example split the address at a 4 KiB boundary: virtual page base 0x10000000, offset 0x040. On a hit in the translation lookaside buffer (TLB), the CPU can reuse the cached translation and permissions. If the translation is missing, x86 hardware walks the page tables. A valid, permitted entry lets the load proceed. If the page needs kernel intervention, the CPU raises a page fault; Linux can sometimes establish the mapping and retry. Linux guide to page tables.
Who fetches the data? In our drawing the physical frame begins at 0x025AB000. Keep the same offset and the physical byte is 0x025AB040. A CPU cache can already hold the line containing that byte. If the cache hierarchy and other cores cannot supply it, the memory controller can issue a DRAM read. On a DIMM-based system it uses the machine’s physical address mapping to select a channel, rank, bank, row and column. The C pointer alone cannot tell you which DIMM. Translation and cache lookup are shown separately so you can follow them; parts can overlap in hardware.
To follow the mapping from allocation through first touch, read Linux Memory Allocation. For how page size and NUMA placement affect real workloads, see Linux System Programming for AI Kernels.
Whose stack? Each thread normally runs with its own call stack. Threads in one process share its address space, including heap allocations. In the example a worker thread’s stack is visible in the same rack, but the helper’s call creates a frame only on the main thread. A typical Linux implementation can keep a stack page mapped after a return. The C lifetime of the local object ends regardless.
Where does malloc get an address? The allocator can carve a block out of an existing arena. If it needs more space it can obtain or extend a region, for example with brk or anonymous mmap. An individual allocation can be much smaller than a page. Freeing our single byte gives that block back to the allocator; it need not immediately return physical RAM to Linux.
Does a full stack spill onto disk?
A stack limit and memory pressure are different events. If a thread exhausts its permitted stack extent, further access can hit a guard page or fail stack growth and generate SIGSEGV. Adding swap does not extend that address range. Separately, under memory pressure Linux may evict an eligible anonymous page, including one containing stack data, to configured swap. A later access can fault it back in at the same virtual address. Neither event moves a C local object onto the heap. Linux resource limits · Linux page cache and reclaim.
Where does mmap fit?
Imagine mapping the first page of a sensor recording read-only. The returned address names a virtual range associated with the file. Reading the first byte can fault in a page-table entry. If the file page is already in the page cache, the read can be served from RAM; otherwise Linux may have to fetch the data from storage. A clean file-backed page can be discarded and read again from the file. Anonymous memory has no original file to reread. mmap on Linux.
To inspect an actual layout, run cat /proc/$$/maps in a Linux shell. This shows the shell’s mappings: start address, exclusive end, permissions and, where applicable, a file path. Look for the main [stack] and a file-backed r-xp mapping. This list describes virtual regions; it does not tell you which individual pages are resident. Addresses vary across runs, and thread stacks need not have a visible stack label. Reading /proc/pid/maps.
For the next level down, see Linux Memory Allocation: malloc, mmap, Pages, TLBs, and Huge Pages.
A pointer also has a time limit
Our local array stays alive while main runs. Passing a pointer to a helper that reads the array during the call works. Returning a pointer to a helper’s own local array and using it after the helper returns does not: the array’s lifetime has ended. A useful sketch for a real interface has one more annotation beside the arrow: who owns the target, and until when may the caller use it?
Run the experiment
Save this as pointers.c. The assertions check both the pointer that stayed on the first reading and the one that moved. Predict the two output lines before running it.
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
int main(void)
{
int samples[] = {18, 25, 31, 28};
size_t count = sizeof samples / sizeof samples[0];
int *p = samples;
int *first = p;
int **pp = &p;
++p;
assert(*first == 18 && **pp == 25);
**pp = 42;
assert(samples[1] == 42);
for (const int *it = samples; it != samples + count; ++it)
printf("%d ", *it);
putchar('\n');
char greeting[] = "hello";
const char *names[] = {"red", "blue", "tan"};
assert(sizeof greeting == 6 && greeting[5] == '\0');
assert(names[1][1] == 'l');
printf("%c, %zu bytes in greeting\n", names[1][1], sizeof greeting);
return 0;
}gcc -std=c17 -Wall -Wextra -Wpedantic pointers.c -o pointers
./pointersThe readings print as 18 42 31 28, followed by l, 6 bytes in greeting. As a final exercise, move ++p; one line earlier, ahead of the assignment to first. Which assertion would you change, and why? You can answer that by moving one arrow on paper.
Advanced example: pointers inside our AI runtime
C-Kernel-Engine (CKE) is an AI runtime we developed for our own work. A runtime is the part of the system that actually runs a model. In CKE, we describe the model’s calculations, plan where its data will live, and generate C code that calls small numerical routines called kernels. One kernel might multiply two matrices. To do that, it needs to know where the input numbers are and where to write the result.
You already know the two ideas we’ll use here. A data pointer leads to a buffer, like p leading to the readings array. A function pointer leads to the code we want a worker to run. Let’s follow one of each. For background on the project, see How to Get Started With C-Kernel-Engine on Linux or browse the CKE source repository.
A pointer can also select the work to run
So far our arrows have ended at data. A thread pool has another question: once a worker wakes up, which function should it call? C lets us store the answer in a function pointer. Read void (*fn)(int, int, void *) from the name outward: fn is a pointer, its target accepts three arguments, and that function returns void. The parentheses matter: void *fn(...) would declare a function returning a data pointer.
typedef void (*ck_work_fn_t)(int ith, int nth, void *args);CKE gives that type a name. A compatible function can be assigned to a variable of that type and invoked as fn(ith, active, args). The first two values identify the worker and the active worker count. The third is a separate pointer to job data. The function signature stays fixed while different jobs supply different buffers and shapes. The caller and callback must agree on the actual context type and keep it alive through completion. Callback types in CKE · The worker calling fn.
One base, three tensor views
Here a tensor is an array of numbers with a shape, such as a matrix with 16 rows and 8 columns. The kernel needs both a pointer to its first element and the layout that tells it how to find the others. In one CKE v8 generation path, the generator turns layout entries into runtime byte offsets. The C it emits initializes an arena using the total size and region boundaries, saves its base, and derives pointers for activations and (when present) the KV cache. A kernel can receive a ready-to-use pointer without searching a table of tensor names in its inner loop. Layout offset calculation · Generated initialization and typed views.
g_model->activations = (float*)(g_model->bump + BUMP_ACT_OFFSET);Read the parentheses before the cast. Add the planned byte offset to the byte-addressed base. Cast the resulting address to float *. From then on, activations[i] steps in floats. If you cast the base first and then add 64, you move 64 floats, or 256 bytes on this target. That single change can send the kernel into a different buffer.
Why plan this up front? The planner knows the size of each region and can allow for alignment before execution. Moving the whole arena changes the base but leaves offsets between regions intact. If two pieces of work overlap in time, their writable scratch space must stay separate unless sharing is explicitly coordinated. Reusing a range after its last consumer finishes is a lifetime decision, much like knowing when it is safe to free a C object.
Why does this CKE kernel use a flat buffer for a matrix?
Our C-Kernel-Engine still multiplies two-dimensional matrices. Look at the output assignment in its FP32 reference code: C[i * N + j] = (float)sum;. Here C points to the first output float and N is the number of columns. To write row 1, column 0 of a three-column result, the kernel writes element 3. That’s the same layout as the six integers above. A rectangular C array already gives me contiguous rows. The flat interface also lets the runtime hand the kernel a pointer into its planned arena and supply the dimensions at run time, without constructing a table of row addresses. Read the reference GEMM indexing in CKE.
There are two different units in that calculation. The arena offset locates the tensor in bytes. Once the kernel has a float *, the row stride counts floats. For this densely packed output the stride is N. A view with padding between rows would need its actual stride. Each layout has to match what the kernel expects.
Where does the same number live in 1D, 2D and 3D?
Take twelve samples numbered 0 through 11. I want to find the one labelled 7. Suppose the arena starts at 0x1000 and the layout reserves its first 64 bytes for something else. Our samples begin at 0x1040. That starting offset is paid once, however many dimensions we give the samples.
In the packed 3D view, a plane is one two-row grid. Skip one plane (six floats), stay on its first row, then move one column. That lands on slot 7. The strides are 6, 3 and 1: moving one plane, row or column advances that many floats. Grouping the same twelve values into rows or planes changes the coordinates we use to find them. Their order in memory stays the same.
Now give each three-value row a spare slot. In C I could declare float storage[4][4] and use only the first three columns of each row. I’ve chosen that padding explicitly. An ordinary float a[4][3] packs its rows together. The padded allocation is still contiguous, but its twelve useful values have gaps between rows. Row 2 now starts at slot 8, so its second value lives at slot 9. Pass a stride of 3 by mistake and the kernel reads slot 7: in this layout that’s padding.
What about float **? The earlier pointer-table diagram shows the difference. A table stores a row address in each slot, and its rows can live in separate allocations. A true float a[2][2][3] contains the floats themselves and stores no row or plane pointers. CKE’s packed output example needs only a base pointer and the row width because its kernel and caller agree on that layout.
Does CKE actually declare a 2D array?
Yes. The blocked BF16 matrix multiply receives its main buffers as pointers, then declares a small working tile inside the kernel: float acc[BLK_M][BLK_N];. Here acc[i][j] really does index a C array of rows. Both choices appear in the same function. The flat pointers let the caller supply the matrix storage; the tile gives the kernel a fixed-size rectangle for its partial sums. See the accumulator and input row pointers side by side.
For a 3D example, look at the attention keys. The three coordinates are head, token and feature. CKE first computes the start of the head and then picks the token and feature within it. Written as one offset, that’s (head * key_tokens + token) * head_dim + feature. The tensor has three dimensions even though the C kernel reads through a plain float pointer. The 3D strip above illustrates that indexing idea; its two planes and twelve samples are teaching sizes. Follow the attention key address in CKE.
Follow one token from generated C into the kernel
Here’s where I can put a finger on the bytes. I opened the generated model_v8.c for a Qwen2-0.5B-Instruct decode build. Its model struct holds a uint8_t *bump. Adding 1 to that pointer advances one byte. The generated calls add a different offset for each operand, then pass the resulting pointer and dimensions to the kernel. These offsets belong to this build.
#define W_LAYER_0_LN1_GAMMA 149355180
#define W_TOKEN_EMB 496
#define A_TOKEN_IDS 397697708
#define A_EMBEDDED_INPUT 397701804
#define A_RESIDUAL 4066638521. Write the token ID, then look up its row. The generated decoder writes the incoming integer at byte offset 397697708. Here is the call immediately after that store. Read its pointers in order: token IDs, packed embedding weights, optional position embeddings, and the FP32 destination. The final arguments tell the kernel that this call handles one token with 896 features and no added position embedding.
embedding_forward_q8_0(
(int32_t*)(model->bump + A_TOKEN_IDS),
1,
151936,
(const void*)(model->bump + W_TOKEN_EMB),
NULL,
(float*)(model->bump + A_EMBEDDED_INPUT),
896,
896,
1,
0
);Inside the kernel, id = token_ids[t] selects the vocabulary row. The key line is tok = base + (size_t)id * row_bytes. This table stores Q8_0 blocks: 32 quantized values and a two-byte scale per block. One row occupies 28 × 34 = 952 bytes. For token ID 1, the read starts at arena offset 496 + 952 = 1448. The kernel decodes those bytes into 896 floats beginning at offset 397701804. Same feature count, two different storage sizes. Read the embedding kernel.
2. Save a copy, then normalize in place. The next generated operation copies 3,584 bytes (896 × 4) from the embedded input to A_RESIDUAL. Look at arguments one and three below: the decoder passes the same address as both input and output. The saved residual keeps the original values for later.
rmsnorm_forward(
(const float*)(model->bump + A_EMBEDDED_INPUT),
(float*)(model->bump + W_LAYER_0_LN1_GAMMA),
(float*)(model->bump + A_EMBEDDED_INPUT),
NULL,
1,
896,
896,
9.999999974752427e-07
);The RMSNorm wrapper passes 896 as both input and output row stride. Inside the implementation, x = input + t * input_stride selects the token row, and x[d] selects a feature. Those additions count floats. In this one-token call t is zero, so feature 7 starts at byte offset 397701804 + 7 × 4 = 397701832. A vectorized loop can load several adjacent features at once using the same row address. RMSNorm wrapper · Row selection and vector load.
A small check with known values. I replayed these two native kernel calls and the intervening copy with the emitted offsets and synthetic weights. I set token 1’s row to decode to 3 in every lane and set each normalization weight to 2. All 896 destination values were approximately 2 after normalization; all 896 residual values were still 3. This checks the pointer handoff in isolation. The generated excerpts come from the local Qwen2 decode artifact (SHA-256 prefix 7f7e01f259f9). See how the generator binds the arena.
Try the callback and the arena together
Save this as arena.c and run cc -std=c17 -Wall -Wextra arena.c -o arena && ./arena. The program calls the same function twice with different ranges, sequentially so the pointer arithmetic is easy to inspect. Its first 64 bytes are reserved, and it uses the activation and scratch regions of the diagram. A passing run is silent.
#include <assert.h>
#include <stddef.h>
#include <stdlib.h>
typedef void (*range_fn)(size_t begin, size_t end, void *context);
struct job {
const float *input;
float *out;
};
static void double_values(size_t begin, size_t end, void *context)
{
struct job *j = context;
for (size_t i = begin; i < end; ++i)
j->out[i] = 2.0f * j->input[i];
}
int main(void)
{
_Static_assert(sizeof(float) == 4, "example uses four-byte floats");
unsigned char *base = malloc(128);
if (!base) return 1;
float *activation = (float *)(base + 64);
float *scratch = (float *)(base + 96);
for (size_t i = 0; i < 8; ++i) activation[i] = (float)i;
struct job j = {activation, scratch};
range_fn fn = double_values;
fn(0, 4, &j);
fn(4, 8, &j);
for (size_t i = 0; i < 8; ++i) assert(scratch[i] == 2.0f * i);
free(base);
return 0;
}Follow fn to the executable function, context to j, and j.out to byte offset 96. Only base owns the allocation. Free it once, after both calls finish. Extending this to actual workers introduces scheduling and synchronization; the same address calculation alone gives no thread-safety guarantee.
Follow the runtime into Linux
- How workers receive a job and split a range: Thread Pools in C: How CPU Runtimes Dispatch Work Across Cores.
- Who owns workers and reusable memory: Threadpools And Memory Pools: Why CKE Needs Runtime Ownership For CPU AI Kernels.
- What backs the virtual address range: Linux Memory Allocation: malloc, mmap, Pages, TLBs, and Huge Pages.
- Where placement and locality start to matter: Linux System Programming For AI Kernels: Core Pinning, Huge Pages, TLBs, NUMA, and Memory Discipline.
- Where generated C fits into serving: From Generated C to Useful Agents: CKE Serving Qwen Code and Gemma4.
For the book behind the pointer drawings and a four-reading C project to try, see Four Books I Recommend to Learn C.
Comments
Join the conversation. Replies can go three levels deep.