All lessonsOpen Open Open Open Open
8. Pointers
Pointers
0 of 5 activities0%
Reading 1
An address, not the value
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; // 20Check 2
What & does
If int x = 5; int* p = &x; what does p store?
Fill in 3
Follow the pointer
Try it 4
Change x through a pointer
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
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…