Memory Lesson 12 of 16
C Pointers Explained
Understand memory addresses, the & and * operators, pointer variables, passing pointers to functions to change variables, pointers and arrays, and NULL.
Pointers are what make C special, and what many students find hardest. The good news: the idea is simple. A pointer is a variable that stores a memory address, the location of another variable.
Addresses
Every variable lives somewhere in memory, and that place has an address, like a house number on a street. The & operator gives a variable's address:
#include <stdio.h>
int main(void) {
int age = 16;
printf("Value: %d\n", age);
printf("Address: %p\n", (void *) &age);
return 0;
}
Output
Value: 16 Address: 0x16fdff27c
The address will be different on your computer and on every run. %p prints it in hexadecimal.
Pointer variables
#include <stdio.h>
int main(void) {
int age = 16;
int *p = &age; /* p is a pointer to an int, holding age's address */
printf("age = %d\n", age);
printf("*p = %d\n", *p); /* * reads the value at the address */
*p = 17; /* change age THROUGH the pointer */
printf("age is now %d\n", age);
return 0;
}
Output
age = 16 *p = 16 age is now 17
int *pdeclarespas a "pointer to int".&agemeans "address of age".*pmeans "the value at the address in p". This is called dereferencing.
Think of p as a note that says "the box is at house #27". *p means "go to house #27 and open the box".
Why pointers? Changing variables in functions
Functions receive copies of normal variables. To let a function change the original, pass its address:
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void add_interest(double *balance, double rate) {
*balance = *balance * (1 + rate);
}
int main(void) {
int x = 3, y = 8;
swap(&x, &y);
printf("x = %d, y = %d\n", x, y);
double savings = 1000;
add_interest(&savings, 0.05);
printf("Savings: %.2f\n", savings);
return 0;
}
Output
x = 8, y = 3 Savings: 1050.00
This is exactly why scanf("%d", &age) needs &: scanf must store the value into your variable, so it needs the address.
Returning several results
#include <stdio.h>
void min_max(const int arr[], int n, int *min, int *max) {
*min = *max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] < *min) *min = arr[i];
if (arr[i] > *max) *max = arr[i];
}
}
int main(void) {
int g[] = {88, 72, 95, 81};
int lo, hi;
min_max(g, 4, &lo, &hi);
printf("Lowest %d, highest %d\n", lo, hi);
return 0;
}
Output
Lowest 72, highest 95
Pointers and arrays
An array's name acts like a pointer to its first element. Adding to a pointer moves it forward by whole elements:
#include <stdio.h>
int main(void) {
int nums[] = {10, 20, 30, 40};
int *p = nums; /* same as &nums[0] */
printf("%d %d %d\n", *p, *(p + 1), p[2]);
int sum = 0;
for (int *q = nums; q < nums + 4; q++) {
sum += *q;
}
printf("Sum: %d\n", sum);
return 0;
}
Output
10 20 30 Sum: 100
p[2] and *(p + 2) mean the same thing. This is called pointer arithmetic.
NULL
NULL is a special pointer value meaning "points to nothing". Set pointers to NULL when they don't have a target yet, and check before using them:
int *p = NULL;
if (p != NULL) {
printf("%d\n", *p);
}
Pointer cheat sheet
| Code | Meaning |
|---|---|
int *p; | p is a pointer to an int |
p = &x; | p now holds x's address |
*p | the value at that address (x) |
*p = 5; | sets x to 5 |
p + 1 | address of the next element |
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04