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: 10

The 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: 18

Three 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 error incompatible 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. score stays 10. When a function "does nothing", check the stars first.

Instructions

The function add_coins should add coins, but the program prints Coins: 80, lives: 3, because the function changes only its copy. Change it so that it changes coins in main: make its first parameter a pointer int *coins, add the star inside the function and pass the address &coins in the call. The program then prints Coins: 130, lives: 3.

In the game, a life costs 100 coins. Below add_coins, write a function buy_life with two parameters of type int *: coins and lives. Through the pointers, it subtracts 100 coins and adds one life. At the end of main (before return 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 prints Coins: 130, lives: 3 and then Coins: 30, lives: 4.

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 prints Not enough coins. Then call buy_life once more and print the line with the coins and lives again. The last three lines of the output are Coins: 30, lives: 4, Not enough coins and Coins: 30, lives: 4.

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

5/11