Pointers and Arrays

In C, pointers and arrays are close friends. The name of an array, used on its own, is the address of its first element. So you can store it in a pointer, without &:

#include <stdio.h>

int main(void) {
    int numbers[] = {3, 7, 1};
    int *p = numbers;
    printf("%d\n", *p);
    printf("%d\n", p[1]);
    return 0;
}

This prints:

3
7

int *p = numbers; means the same as int *p = &numbers[0];, so *p is the first element. And you can use square brackets with a pointer, just like with the array: p[1] is the element right after the one p points to, here 7.

The elements of an array lie right next to each other in memory. p + 1 is the address of the next element, and p[1] is a shorter way to write *(p + 1). This course uses the square brackets, but you'll see the other form in other C programs.

Remember the error from the lesson on arrays: printing a whole array with printf("%d\n", scores); doesn't compile, and the message says the argument has type 'int *'. Now you know why: the name of the array is an address, of the type int *, and not a number for %d.

This also explains something you saw in the lesson on functions. When you pass an array to a function, C doesn't copy it. It passes only the address of its first element. A parameter int numbers[] is in fact a pointer, so you can also write it as int *numbers. Both mean exactly the same:

#include <stdio.h>

void double_all(int *numbers, int count) {
    for (int i = 0; i < count; i++) {
        numbers[i] *= 2;
    }
}

int main(void) {
    int prices[] = {10, 25, 40};
    double_all(prices, 3);
    printf("%d %d %d\n", prices[0], prices[1], prices[2]);
    return 0;
}

This prints:

20 50 80

Through the address, the function works on the caller's array, so its changes stay. That's also why the function needs the count: it gets only the address of the first element, and it can't tell where the array ends. Even sizeof(numbers) / sizeof(numbers[0]) doesn't help inside the function. There, numbers is a pointer, so sizeof gives the size of the pointer, and the result is wrong (here it's 1). Only where the array was created, as in main, does sizeof see the whole array. So always pass the count as a parameter.

Instructions

At the end of main (before return 0;), create a pointer p to the first element of scores: int *p = scores;.

Below it, print two elements through the pointer: the text First: and *p, and on the next line Second: and p[1]. The program prints First: 12 and Second: 7.

Change the parameter of print_all from int numbers[] to int *numbers. The program prints the same as before, because both mean the same.

Below print_all, write a function add_to_all with the parameters int *numbers, int count and int amount, which adds amount to every element. At the end of main, call it with scores, 4 and 5, and then call print_all(scores, 4) once more. The last line of the output is 17 12 20 14.

Start programming for free

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

Or sign up with:

8/11