Lesson 18 of 23

Pointers: Working with Addresses

12 min 35 XP

Every variable lives at an address in memory; &x gives that address. A pointer is a variable that stores an address: int *p = &x;. *p reads or writes the value at that address (dereferencing).

Example: edit me and press Run

Output Loading C (first time takes a few seconds)
Press Run to see the result here.

Dereferencing a NULL pointer crashes the program (a segmentation fault). Check for NULL before using a pointer.

Challenge

+35 XP

Read two numbers. Using pointers int *pa = &a, *pb = &b, compute the sum through *pa and *pb, store it back into a through *pa, and print: Input: 4 9 Output: Sum: 13 a: 13

Output Loading C (first time takes a few seconds)
Press Run to see the result here.