Pointers as Parameters
So far, the pointer and its variable were both in main, and you could have changed the variable directly. Now comes the main reason why pointers exist. Remember from the lesson on functions: a parameter is a copy of the argument. When a function changes its parameter, the caller's variable stays the same:
#include <stdio.h>
void add_bonus(int points, int bonus) {
points += bonus;
}
int main(void) {
int score = 10;
add_bonus(score, 5);
printf("Score: %d\n", score);
return 0;
}This prints:
Score: 10The function got the number 10, not the variable score. It's like giving someone a photocopy of your test: whatever they write on the copy, your original stays as it was.
The solution: give the function the address of the variable. With the address, the function can reach the caller's box and change what's inside:
#include <stdio.h>
void add_bonus(int *points, int bonus) {
*points += bonus;
}
int main(void) {
int score = 10;
add_bonus(&score, 5);
add_bonus(&score, 3);
printf("Score: %d\n", score);
return 0;
}This prints:
Score: 18Three things changed: the parameter is a pointer, int *points; inside the function, *points is the caller's variable; and the call passes an address, &score. The address is copied into the parameter too, but a copy of an address still leads to the same box. It's like giving someone your home address on a piece of paper: with a copy of the paper, they still find the same house.
In C, this is the usual way for a function to change its caller's variables. There's one more advantage: the call itself shows that the function may change score, because you see the &. The parameter bonus stays an ordinary int, because the function only reads it.
Common mistakes:
- A call without
&,add_bonus(score, 5);, ends with the errorincompatible integer to pointer conversion passing 'int' to parameter of type 'int *'; take the address with &. The compiler tells you what to do: take the address with&. - When you forget the star inside the function and write
points += bonus;, the program compiles without any error, but it changes the copy of the address, the pointer itself, and not the score.scorestays 10. When a function "does nothing", check the stars first.
Instructions
- 1
The function
add_coinsshould add coins, but the program printsCoins: 80, lives: 3, because the function changes only its copy. Change it so that it changescoinsinmain: make its first parameter a pointerint *coins, add the star inside the function and pass the address&coinsin the call. The program then printsCoins: 130, lives: 3. - 2
In the game, a life costs 100 coins. Below
add_coins, write a functionbuy_lifewith two parameters of typeint *:coinsandlives. Through the pointers, it subtracts 100 coins and adds one life. At the end ofmain(beforereturn 0;), call it with the addresses of both variables and then print the line with the coins and lives again, the same way. The program printsCoins: 130, lives: 3and thenCoins: 30, lives: 4. - 3
A life can be bought only with at least 100 coins. Add a condition to
buy_life: when there are fewer than 100 coins, the function changes nothing and printsNot enough coins. Then callbuy_lifeonce more and print the line with the coins and lives again. The last three lines of the output areCoins: 30, lives: 4,Not enough coinsandCoins: 30, lives: 4.