Addresses and Pointers
Besides references, C++ has one more way to reach a variable from somewhere else: pointers. To understand them, you need to know what an address is.
Every variable lives in the computer's memory. You can picture memory as a long row of numbered boxes, and the number of the box where a variable lives is called its address. You get the address of a variable with the & sign before its name:
#include <iostream>
int main() {
int age = 15;
std::cout << age << std::endl;
std::cout << &age << std::endl;
return 0;
}This prints, for example:
15
0xfff4The address is printed as a hexadecimal number, which is why it starts with 0x and can have letters among the digits. On your computer it will be different and longer, such as 0x7ffc1b2a3c4c. You don't need to care which number it is. What matters is that you can store an address in a variable and reach the variable through it.
A variable that stores an address is called a pointer. Its type is the type of the variable it points to, followed by an asterisk. A pointer to an int has the type int*, a pointer to a double the type double*:
int age = 15;
int* pointer = &age;Read the second line as "pointer is a pointer to an int and holds the address of the variable age". We say that pointer points to age.
You reach the variable a pointer points to with an asterisk before the pointer: *pointer. This way you can both read it and write to it:
#include <iostream>
int main() {
int age = 15;
int* pointer = &age;
std::cout << *pointer << std::endl;
*pointer = 16;
std::cout << age << std::endl;
return 0;
}This prints:
15
16The statement *pointer = 16; wrote 16 into the variable the pointer points to, which is age. It's similar to a reference, but you have to write the asterisk every time.
Common mistakes:
int* pointer = age;without&ends with the errorcannot initialize a variable of type 'int *' with an lvalue of type 'int': a pointer holds an address, not a number.pointer = 16;without the asterisk ends with the errorincompatible integer to pointer conversion assigning to 'int *' from 'int': you are trying to store a number in the pointer itself instead of in the variable it points to.- The type of the pointer must match the type of the variable.
double price = 9.5;followed byint* pointer = &price;ends with the errorcannot initialize a variable of type 'int *' with an rvalue of type 'double *'. Adoubleneeds adouble*. - When you print
pointerwithout the asterisk, the program doesn't crash, but it prints the address instead of the value, such as0xfff4.
Instructions
Below the variable value, create a pointer of type int* named pointer that points to value.
Below it, use the pointer to change the variable's value to 10. Don't write to value directly: write to it through *pointer. The program then prints Variable value: 10.
At the end of the program (before return 0;), print through the pointer: the text Through the pointer: and the value that pointer points to. The program prints Variable value: 10 and then Through the pointer: 10.