C Pointers
A pointer is a variable that stores a memory address. Instead of holding a number like 42 directly, it holds the location of another variable that contains 42. Through the pointer you can read that variable and change it — even from a different function.
Declaring a pointer
A pointer declaration uses * after the type it points to:
int *p; // p is a pointer to an int
double *q; // q is a pointer to a double
char *s; // s is a pointer to a charYou make a pointer point somewhere by assigning it an address with &. To reach the value at that address, you dereference the pointer with *:
#include <stdio.h>
int main(void) {
int apples = 12;
int *p = &apples; // p now holds the address of apples
printf("apples = %d\n", apples);
printf("*p = %d\n", *p); // read through the pointer
*p = 20; // write through the pointer
printf("apples is now %d\n", apples);
return 0;
}apples = 12
*p = 12
apples is now 20
The assignment *p = 20 did not change p; it changed the variable p points to.
The two meanings of *
The star is the most confusing symbol in C because it has two jobs:
| Where | Example | Meaning |
|---|---|---|
| In a declaration | int *p; |
"p is a pointer to int" |
| In an expression | *p = 5; |
"the thing p points to" (dereference) |
| Between two values | a * b |
ordinary multiplication |
And & means "address of": &x is a pointer to x. So *&x is just x again.
Pointers can be re-aimed
A pointer is a variable, so you can make it point to something else later:
#include <stdio.h>
int main(void) {
int red = 1, green = 2, blue = 3;
int *current = &red;
printf("current points to %d\n", *current);
current = &blue;
printf("current points to %d\n", *current);
*current += 10;
printf("red=%d green=%d blue=%d\n", red, green, blue);
return 0;
}current points to 1
current points to 3
red=1 green=2 blue=13
Why pointers? Changing a caller's variables
C passes function arguments by value: the function gets a copy. To let a function modify your variables, you pass their addresses. This is the classic swap function:
#include <stdio.h>
void swap_copies(int a, int b) { // works on copies — useless
int t = a; a = b; b = t;
}
void swap(int *a, int *b) { // works on the originals
int t = *a;
*a = *b;
*b = t;
}
int main(void) {
int x = 1, y = 2;
swap_copies(x, y);
printf("after swap_copies: x=%d y=%d\n", x, y);
swap(&x, &y);
printf("after swap: x=%d y=%d\n", x, y);
return 0;
}after swap_copies: x=1 y=2
after swap: x=2 y=1
Pointers also let functions return several results at once (through "output parameters"), avoid copying large data, and build dynamic data structures. Functions are explained in detail in the C Functions section.
The null pointer
A pointer that points nowhere should hold NULL (from <stdio.h>, <stdlib.h> and others). Many functions return NULL to report "not found" or "failed". Always check before dereferencing:
#include <stdio.h>
int main(void) {
int value = 7;
int *maybe = NULL;
if (maybe == NULL) {
printf("maybe points nowhere yet\n");
}
maybe = &value;
if (maybe != NULL) {
printf("maybe points to %d\n", *maybe);
}
return 0;
}maybe points nowhere yet
maybe points to 7
Dereferencing NULL crashes the program (typically a "segmentation fault").
Pointer dangers
| Bug | What happens | Prevention |
|---|---|---|
Uninitialised pointer: int *p; *p = 5; |
writes to a random address | initialise every pointer, to NULL if nothing else |
| Null dereference | crash | check for NULL first |
| Dangling pointer (points to a variable that no longer exists) | reads garbage or crashes | never return the address of a local variable |
Wrong type: double *d = &some_int; |
misinterprets the bytes | let the compiler warnings guide you |
int *broken(void) {
int local = 5;
return &local; // warning: function returns address of local variable
} // local is destroyed here — the pointer danglesTip: Read pointer declarations right-to-left:
const int *pis "p is a pointer to an int that is const" — you can movep, but not change*pthrough it.
Test yourself
Given int n = 4; int *p = &n;, what does *p = 9; do?
- Makes p point to 9
- Sets n to 9
- Is a compile error
- Multiplies p by 9
Why must swap take int * parameters to swap two caller variables?
- Pointers are faster
- C passes arguments by value, so the function needs addresses to modify the originals
- int parameters are not allowed
- It is just a style choice
What value should a pointer have when it does not point to anything valid?
- 0x1
- NULL
- -1
- Any value; it does not matter