All lessons

8. Pointers

Pointers

0 of 5 activities0%

Reading 1

An address, not the value

Open

A pointer holds a memory address. Declare it with a * after the type: int* p;

&x is the address of x. *p follows the pointer (dereference) to read or write the value at that address.

Pointers must point somewhere useful before you dereference them. A common pattern is int* p = &x;

int x = 10;
int* p = &x;

cout << *p << endl;  // 10
*p = 20;
cout << x << endl;   // 20

Check 2

What & does

Open

If int x = 5; int* p = &x; what does p store?

Fill in 3

Follow the pointer

Open

The operator that reads the value a pointer points to is

Try it 4

Change x through a pointer

Open

Run it. Then set *p to another value and print x again.

main.cpp
Loading editor…
Output will appear here.

Assignment 5

Print via pointer

Open

Read one integer into a variable n. Create a pointer to n and print the value using the pointer (not n directly). End with a newline.

main.cpp
Loading editor…