Notes › C
Pointers in C
A pointer is a variable that stores a memory address. Pointers let C functions change their caller's variables, work with arrays and strings efficiently, and manage memory directly.
& and *
int x = 10;
int *p = &x; // p holds the address of x
printf("%d\n", x); // 10
printf("%p\n", (void*)p); // e.g. 0x7ffd5c1c (address)
printf("%d\n", *p); // 10 — *p reads the value at that address (dereference)
*p = 20; // changes x through the pointer
printf("%d\n", x); // 20
| Symbol | In a declaration | In an expression |
|---|---|---|
* |
"p is a pointer" (int *p) |
"value at p" (dereference) |
& |
— | "address of" |
Size of a pointer
All data pointers on a system have the same size — usually 8 bytes on 64-bit and 4 bytes on 32-bit — whatever they point to. The pointer's type tells the compiler how many bytes to read and how far p + 1 moves.
Pointer arithmetic
int a[4] = {10, 20, 30, 40};
int *p = a; // same as &a[0]
printf("%d\n", *(p + 2)); // 30
p++; // moves by sizeof(int) bytes, now points to a[1]
printf("%d\n", *p); // 20
printf("%td\n", &a[3] - &a[0]); // 3 (elements, not bytes)
a[i] is exactly *(a + i). That's also why i[a] works (don't write it!).
Arrays vs pointers
Array int a[5] |
Pointer int *p |
|
|---|---|---|
sizeof |
20 (5 × 4) | 8 |
| Can be re-assigned | ❌ | ✅ |
| Stores | the elements | one address |
When passed to a function, an array decays to a pointer — the function can't know its length, so pass the size too:
int sum(int *arr, int n) {
int s = 0;
for (int i = 0; i < n; i++) s += arr[i];
return s;
}
Call by value vs call by reference
C always passes values; passing an address simulates "by reference".
void swapWrong(int a, int b) { int t = a; a = b; b = t; } // changes copies only
void swap(int *a, int *b) { int t = *a; *a = *b; *b = t; } // changes the originals
int x = 1, y = 2;
swap(&x, &y); // x = 2, y = 1
Pointers and strings
char s[] = "hello"; // array: modifiable copy
char *t = "hello"; // pointer to a string literal: read-only!
s[0] = 'H'; // OK
// t[0] = 'H'; // undefined behaviour (often a crash)
int len = 0;
while (s[len] != '\0') len++; // strings end with the null character
Pointer to pointer
int x = 5;
int *p = &x;
int **pp = &p;
printf("%d\n", **pp); // 5
Used for 2D dynamic arrays and for functions that must change a pointer (e.g. inserting at the head of a linked list).
Special pointers
| Pointer | Meaning | Danger |
|---|---|---|
| NULL | points nowhere (NULL / 0) |
dereferencing it crashes — always check |
| Dangling | points to memory that was freed or went out of scope | reading it is undefined behaviour |
| Wild | declared but never initialised | random address |
| void * | generic pointer (malloc returns one) |
must be cast before dereferencing |
int *bad() {
int local = 5;
return &local; // ❌ dangling: local dies when the function returns
}
const with pointers
const int *p; // can't change *p (the value); can change p
int *const q = &x; // can't change q (the address); can change *q
const int *const r = &x; // neither
Read it right-to-left: "q is a const pointer to int".
Function pointers
int add(int a, int b) { return a + b; }
int (*op)(int, int) = add;
printf("%d\n", op(2, 3)); // 5
Used for callbacks, e.g. the comparator of qsort.
Interview one-liners
- A pointer stores an address;
*dereferences,&takes an address. p + 1moves bysizeof(*p)bytes.- Arrays decay to pointers when passed to functions.
- Set pointers to
NULLafterfreeto avoid dangling pointers. - Next: Dynamic Memory in C, Structures and Unions in C.