Loading…
Loading…
Charting your lesson…
Opening your lesson…
Pointers and Memory Addressing
Lesson 2 of 4
6 steps•about 12 min•12 flashcards
Step 1/ 61 min
Pointers are the reason C is both powerful and feared. They are not "hard" — they are one idea applied consistently: a pointer is a variable whose value is a memory address.
Core idea: every byte of memory has a numeric address. A pointer stores one such address. The & operator asks "what is the address of this object?" and the * operator asks "what object lives at this address?". They are inverses: *(&x) is x.
Step 2/ 61 min
int x = 42;
int *p = &x; // p holds the address of x
printf("%d\n", *p); // 42 — dereference: read what p points to
*p = 7; // write through the pointer; now x == 7
The type int * means "pointer to int". The pointed-to type matters because it tells the compiler how many bytes to read on a dereference and how far one step of pointer arithmetic moves.
Adding 1 to an int * advances by sizeof(int) bytes, not one byte. So p + 3 is the address of the third int after p. This is exactly why a[i] is defined as *(a + i) — array indexing is pointer arithmetic. A curious consequence: a[i] == *(a+i) == *(i+a) == i[a], so 3[arr] compiles (though never write it in production).
A double **pp is a pointer to a pointer — needed when a function must modify the caller's pointer (e.g. allocating and returning via an out-parameter). Two lethal states:
void alloc_row(int **out, int n) { // modify caller's pointer via **
*out = malloc(n * sizeof(int));
}
Step 3/ 61 min
void swap(int *a, int *b) {
int t = *a; *a = *b; *b = t;
}
int x = 1, y = 2;
swap(&x, &y); // pass ADDRESSES; now x == 2, y == 1
Passing x, y by value would swap only local copies and leave the originals untouched. Passing &x, &y lets swap reach back into the caller's variables. This is how C simulates pass-by-reference.
Step 4/ 61 min
"Why does my swap not work?" (answer: passing by value), "what is sizeof(p) vs sizeof(*p)", and pointer-arithmetic output questions are staples. Understanding that sizeof(pointer) is the address width (8 bytes on 64-bit), regardless of the pointed-to type, is a common differentiator.
Step 5/ 61 min
sizeof(p) is the pointer's own size (8 on 64-bit); sizeof(*p) is the pointed-to type's size.free(p), set p = NULL to avoid a dangling dereference or double-free.Confusing * in a declaration with * as the dereference operator. In int *p; the star is part of the type ("p is a pointer to int"); in *p = 5; it dereferences. They read identically but do opposite jobs — one names a pointer, the other follows one.
& takes an address, * dereferences it.p+1 moves sizeof(*p) bytes.a[i] is literally *(a+i) — indexing is pointer arithmetic.&/* inverse pair, remember arithmetic scales by type size, and defend against NULL, wild and dangling pointers — that is 90% of pointer bugs.Step 6/ 6· Worked examples2 min
Problem
A function must swap two integers through pointers, and a second function must let the caller see a newly allocated array. Trace why the naive versions fail and write the correct code.
void bad_swap(int a, int b) { int t = a; a = b; b = t; } // fails
void good_swap(int *a, int *b) { int t = *a; *a = *b; *b = t; }// works
void bad_alloc(int *p, int n) { p = malloc(n * sizeof *p); } // fails: caller's p unchanged
void good_alloc(int **p, int n){ *p = malloc(n * sizeof **p); }// works
Try it on paper first.
Recall
Keep this
Pointers are the differentiator in C interviews — panels use them to separate candidates who memorised syntax from those who understand memory. Segmentation faults, pointer arithmetic, and const placement are perennial written-test and debugging questions.
A pointer is a variable that stores the memory address of another object. &x gives the address of x; *p dereferences p to read/write the pointed-to value. A pointer's own size is fixed by the architecture (8 bytes on 64-bit), regardless of the type it points to.
Adding 1 to a pointer advances it by sizeof(pointed-type) bytes, not 1 byte. So for int *p, p + 1 moves 4 bytes ahead. This is why array indexing a[i] is exactly *(a + i).
| Declaration | Meaning |
|---|---|
const int *p |
pointer to const int (can't change *p) |
int *const p |
const pointer to int (can't change p) |
const int *const p |
both fixed |
a[i] ≡ *(a + i) ≡ *(i + a) ≡ i[a] — the last form actually compiles and is a classic trick question.sizeof(char*) == sizeof(double*).free(p), set p = NULL to avoid a dangling-pointer double-free.const declarations right-to-left from the variable name to decode what is constant.void* is a generic pointer — it can hold any address but must be cast before dereferencing, and pointer arithmetic on it is illegal in standard C.int **pp) is needed to modify a caller's pointer inside a function (pass-by-reference of a pointer).Output-prediction with pointer arithmetic on arrays, decoding const int *const p, or "why does this crash?" (dangling/NULL). Swap-two-numbers via pointers is a staple viva question.
A pointer stores an address; & takes an address, * dereferences. Pointer arithmetic scales by sizeof(type). a[i] == *(a+i). Pointer size = architecture width. NULL/dangling/wild pointers cause crashes — always initialize and null after free.
You've read 0 of 6 steps
Now lock it in — questions straight after reading are what make it stick.