What Is a Pointer in C? A Beginner Explanation
Pointers are the topic that makes most C beginners nervous. The good news is that the idea behind them is simple: a pointer is a variable that stores a memory address. Once that sentence clicks, the syntax becomes much easier to read.
Memory has addresses
Every variable lives somewhere in your computer's memory, and every location has an address, like the number on a house in a street. You can ask C for that address with the & operator, which means "address of".
#include <stdio.h>
int main(void)
{
int age = 25;
printf("value: %d\n", age);
printf("address: %p\n", (void *)&age);
return (0);
}
The value is always 25, but the address changes from one run to the next because the operating system decides where to place your program.
Declaring a pointer
A pointer needs a type so C knows what kind of data lives at the address it holds.
int age = 25;
int *ptr = &age;
Read int *ptr as "ptr can hold the address of an int". After the second line, ptr stores the address of age.
Following a pointer with the * operator
Putting * in front of a pointer in an expression means "go to that address and use what is there". This is called dereferencing.
printf("%d\n", *ptr); /* prints 25 */
*ptr = 30;
printf("%d\n", age); /* prints 30 */
Changing *ptr changes age because both names lead to the same piece of memory.
The three symbols to remember:
&xgives the address ofxint *pdeclares a pointer to an int*preads or writes the value thatppoints to
Why do we need pointers?
1. Letting a function change a variable. C passes arguments by value, so a function normally receives a copy. If you pass an address instead, the function can change the original.
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
Calling swap(&x, &y) with x = 1 and y = 2 leaves x as 2 and y as 1.
2. Working with arrays and strings. An array name turns into a pointer to its first element when you pass it to a function, which is how functions like printf and strlen receive strings.
3. Asking for memory at runtime. The malloc function returns a pointer to a block of memory you requested. You can read more about that in our guide to memory leaks in C.
Pointers and arrays
int nums[3] = {10, 20, 30};
int *p = nums;
printf("%d\n", *p); /* 10 */
printf("%d\n", *(p + 1)); /* 20 */
Adding 1 to a pointer moves it to the next element, not the next byte. C multiplies by the size of the type for you, so p + 1 on an int pointer moves forward by sizeof(int) bytes.
NULL: a pointer that points nowhere
When a pointer has no valid address yet, set it to NULL. Dereferencing a NULL pointer crashes your program, so check before you use it.
int *p = NULL;
if (p != NULL)
{
*p = 10;
}
Common beginner mistakes
- Using a pointer before giving it an address
- Dereferencing a NULL pointer
- Forgetting the
&when a function such asscanfexpects an address - Using a pointer after the memory it points to was freed
Most of these end in a crash, which we cover step by step in segmentation faults in C and dangling pointers.
Try it yourself
Write a function void set_zero(int *n) that sets the integer it receives to zero, then call it from main and print the result. If the value changes in main, you have understood pointers.
Want a longer, illustrated walkthrough? Our C Programming notes explain pointers with a street-of-houses analogy and plenty of worked examples.