Show contents
Chapter 4 of 7 · 20 min
Pointers, addresses in memory
By the end of this chapter, you'll be able to
- Get the address of a variable with &
- Declare a pointer and access the value it points to with *
- Write a function that changes the caller's variables
- Explain the link between an array and a pointer
- Recognise a NULL or uninitialised pointer
Addresses and pointers
Each variable occupies a place in memory, identified by a number: its address. Picture a street: the variable is a house, its value is what's inside, and its address is the number on the door.
&xgives the address ofx(you already use it withscanf);int *pdeclaresp, a pointer to an int: a variable that holds the address of anint;*pmeans the variable pointed to: you can read and change it throughp. This is called dereferencing the pointer.
#include <stdio.h>
int main(void) {
int stock = 40;
int *p = &stock; // p holds the address of stock
printf("stock is %d\n", *p);
*p = 35; // we change stock through p
printf("stock is now %d\n", stock);
return 0;
}
stock is 40
stock is now 35
The star therefore has two roles: in a declaration (int *p), it says "p is a pointer"; in an expression (*p = 35), it says "the variable pointed to by p".
A function that changes its arguments
By passing addresses, the function can finally change the caller's variables. It still receives copies, but they are copies of addresses: they lead to the real houses.
Back in main, x and y really have been swapped.
Text description of the animation
- This time, swap receives addresses.
- In main, x is 1 and y is 2.
- We pass the addresses of x and y.
- a points to x and b to y. temp receives *a, that is, the value of x: 1.
- *a = *b writes the value of y into x: x is now 2.
- *b = temp writes 1 into y.
- Back in main, x and y really have been swapped.
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void min_max(const int t[], int n, int *min, int *max) {
*min = t[0];
*max = t[0];
for (int i = 1; i < n; i++) {
if (t[i] < *min) *min = t[i];
if (t[i] > *max) *max = t[i];
}
}
int main(void) {
int x = 1, y = 2;
swap(&x, &y);
printf("x = %d, y = %d\n", x, y);
int sales[] = {120, 80, 310, 95};
int low, high;
min_max(sales, 4, &low, &high);
printf("min = %d, max = %d\n", low, high);
return 0;
}
x = 2, y = 1
min = 80, max = 310
min_max shows another use of pointers: a function can only return one value with return, but it can "return" several by writing to addresses it received as parameters.
Arrays and pointers
The name of an array, used in an expression, turns into the address of its first box (except with sizeof and &: that's why sizeof temp gave the size of the whole array). That's why an array passed to a function isn't copied: the function receives that address. In a parameter, const int t[] and const int *t mean exactly the same thing.
Adding an integer to a pointer moves it forward by that many boxes (not bytes): *(t + 2) is the same thing as t[2].
#include <stdio.h>
int main(void) {
int t[3] = {10, 20, 30};
int *p = t; // same as &t[0]
printf("%d %d %d\n", *p, *(p + 1), p[2]);
return 0;
}
10 20 30
Your turn
Worked exercise
Worked exercise: integer division
Write a function void divide(int a, int b, int *quotient, int *remainder) that stores the quotient and the remainder of a / b at the addresses it receives. In main, display 17 = 5 x 3 + 2 for a = 17 and b = 5.
Hint 1
*quotient = a / b;: the star refers to main's variable.Hint 2
main, declare two ints, then pass their addresses: divide(17, 5, &q, &r);.Show the solution and common mistakes
#include <stdio.h>
void divide(int a, int b, int *quotient, int *remainder) {
*quotient = a / b;
*remainder = a % b;
}
int main(void) {
int q = 0, r = 0;
divide(17, 5, &q, &r);
printf("17 = 5 x %d + %d\n", q, r);
return 0;
}
17 = 5 x 3 + 2
Common mistakes:
- Writing
quotient = a / b;without the star: you change the local copy of the address, notmain's variable. Here, GCC 14 even refuses to compile: you can't store an integer in a pointer. - Passing
qinstead of&q: the function would receive a value to use as an address. The compiler refuses this conversion.
Summary
- 1&x gives the address of x; int *p declares a pointer to an int.
- 2*p refers to the variable pointed to: you can read and change it.
- 3By passing addresses, a function can change the caller's variables.
- 4The name of an array becomes the address of its first box; t[i] is the same as *(t + i).
- 5Always initialise a pointer, and never dereference NULL or an invalid address.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.