To understand pointers, you have to understand how data is stored in a computer's memory. You can think of memory as millions of boxes that you can put things into. Here is a diagram:
Code:
memory: box1
+---------+
| |
| |
| |
+---------+
box2
+---------+
| |
| |
| |
+---------+
box3
+---------+
| |
| |
| |
+---------+
If you create an int variable and assign it a number, then the number gets stored in memory somewhere, i.e. the number gets stored in one of the boxes:
Code:
int num = 10;
memory: box1
+---------+
| |
| |
| |
+---------+
box2
+---------+
| num= |
| 10 |
| |
+---------+
box3
+---------+
| |
| |
| |
+---------+
If you create a pointer to the number, then this happens:
Code:
int num = 10;
int* pint = #
memory: box1
+---------+
| |
| |
| |
+---------+
box2
+---------+
| num= |
pint -------------> | 10 |
| |
+---------+
box3
+---------+
| |
| |
| |
+---------+
You can make pint point to a different box by doing this:
Code:
int age = 7;
pint = &age;
memory: box1
+---------+
| |
| |
| |
+---------+
box2
+---------+
| num= |
| 10 |
| |
+---------+
box3
+---------+
| age= |
pint --------------> | 7 |
| |
+---------+
You can also use pint to change the value in a box:
Code:
*pint = 40;
memory: box1
+---------+
| |
| |
| |
+---------+
box2
+---------+
| num= |
| 10 |
| |
+---------+
box3
+---------+
| age= |
pint --------------> | 40 |
| |
+---------+
By using the const modifier, you can prevent the pointer from changing the value in a box:
Code:
int size = 2;
const int* pint = &size;
*pint = 30; //ERROR
memory: box1
+---------+
const | size= |
pint -------------> | 2 |
| |
+---------+
box2
+---------+
| num= |
| 10 |
| |
+---------+
box3
+---------+
| age= |
pint --------------> | 40 |
| |
+---------+
However, the pointer can still be made to point to another box:
Code:
pint = &age;
memory: box1
+---------+
| size= |
| 2 |
| |
+---------+
box2
+---------+
| num= |
| 10 |
| |
+---------+
box3
+---------+
const | age= |
pint --------------> | 40 |
| |
+---------+
Or, you can use the const modifier to prevent pint from pointing to another box:
Code:
int* const pint = #
pint = &age; //ERROR
memory: box1
+---------+
| size= |
| 2 |
| |
+---------+
box2
+---------+
const | num= |
pint --------------> | 10 |
| |
+---------+
box3
+---------+
| age= |
| 40 |
| |
+---------+
Even though the pointer cannot be made to point to another box, you can still use the pointer to change what's in the box:
Code:
*pint = 35;
memory: box1
+---------+
| size= |
| 2 |
| |
+---------+
box2
+---------+
const | num= |
pint --------------> | 35 |
| |
+---------+
box3
+---------+
| age= |
| 40 |
| |
+---------+
You can also use the const modifier twice:
Code:
const int* const pint = &size;
memory: box1
+---------+
| size= |
| 2 |
| |
+---------+
box2
+---------+
const const | num= |
pint --------------> | 35 |
| |
+---------+
box3
+---------+
| age= |
| 40 |
| |
+---------+
That means that the pointer cannot be moved to point to another box, AND the value in the box cannot be changed:
Code:
pint = &size; //ERROR
*pint = 100; //ERROR
How do you pronounce these pointer names? When you write this:
Code:
const int* pint = &size;
you say, "pint points to a const int". In in other words, you read it from right to left with the * dividing the two parts to be read: "pint" is a pointer to a "const int". And when you write this:
Code:
int* const pint = &size;
you say, "pint is a const pointer to an int". And when you write this:
Code:
const int* const pint = &size;
you say that "pint is a const pointer to a const int".