Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Reading Through a Pointer

A pointer knows where a variable is. To get the value from that box, write * before the pointer: *p means "the value that p points to". Getting a value from an address is called dereferencing a pointer.

#include <stdio.h>

int main(void) {
    int age = 15;
    int *p = &age;
    printf("%d\n", *p);
    printf("%d\n", *p + 1);
    return 0;
}

This prints:

15
16

*p is an int, so you print it with %d and calculate with it like with any whole number.

& and * are opposites. & asks "where is it?" and turns a variable into its address. * asks "what's there?" and turns an address back into the value. That's why *&age is just age again.

A pointer doesn't keep a copy of the value. It knows only the address, and *p looks into the box every time. So when the variable changes, *p gives the new value:

int age = 15;
int *p = &age;
age = 16;
printf("%d\n", *p);

This prints:

16

The star has three meanings now. What matters is where it stands:

  • In a declaration, after a type: int *p = &age; creates a pointer. Here the star says "p is a pointer".
  • Before a pointer, anywhere else: *p is the value it points to.
  • Between two values: price * 2 is multiplication, as always.

So *p * 2 means "the value p points to, times two". The first star gets the value from the address, the second one multiplies.

The typical mistake is forgetting the star. Then you work with the address instead of the value, and the compiler tells you:

  • printf("%d\n", p); ends with the error format specifies type 'int' but the argument has type 'int *': %d expects an int, but p is an address.
  • p * 2 ends with the error invalid operands to binary expression ('int *' and 'int'). Operands are the values around an operator, here p and 2, and you can't multiply an address.

Instructions

  1. 1

    The program doesn't compile. Read the error message and fix the last printf so that it prints the value p points to, not the address. The program prints Score: 50 and Through the pointer: 50.

  2. 2

    Below it, print twice the score, again through the pointer: the text Doubled: and *p * 2. The program prints Doubled: 100 on the next line.

  3. 3

    Below that, change the score directly, without the pointer: score = 60;. Then print the text Now: and *p. The pointer gives the new value: Now: 60.

Back

3/11

  1. 1. Addresses
  2. 2. Pointer Variables
  3. 3. Reading Through a Pointer
  4. 4. Changing Through a Pointer
  5. 5. Pointers as Parameters
  6. 6. Swapping Two Variables
  7. 7. Two Results at Once
  8. 8. Pointers and Arrays
  9. 9. The Null Pointer
  10. 10. Read-Only Pointers
  11. 11. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.