Lesson 26 - Pointers, References, and Constness

Variables and Values

When we declare an int variable, we assign a particular value to it. With this, we could create another int variable that has a copy of the value assigned to the first one. For example, let’s say that:

int i = 4;
int x = i;


Here, both variables i and x are assigned the value 4, but changing the value of i does not change the value of x. That makes sense; they are two different variables… but what if we wanted both x and i to be modified together? In other words, what if we wanted x and i to refer to the same variable?


References

To accomplish this, we create what is called a “reference.” References can be thought of as a sort of alias for the original variable or alternative name for the same value. We declare a reference using the & symbol when declaring the reference’s type. For example:


int i = 4;
int& x = i;


Now, both i and x refer to the exact same object in the memory. 


DRAWING WITH TWO VERTICAL RECTANGLES. THE RIGHT RECTANGLE REPRESENTS MEMORY. LEFT REPRESENTS VARIABLES. THERE WILL BE ARROWS DIRECTED FROM THE LEFT TO THE RIGHT (ie, arrows will span from the variables to the memory). 


So, if we change the value of x like so:


x = 5;


Then, i will also be equal to 5 since both i and x are different names for the same value. We’ll see how this can be useful in our next lesson.


Pointers

Pointers are what we use to directly reference an object’s memory value (its location in memory). We declare a pointer by using the * symbol when declaring the pointer’s type. For example:


int i = 4;
int* y = &i.


Now, line 2 seems like it has a lot going on so let’s break it down.


int* y declares that “y is a pointer to an int variable.”

The &i here is not the same as the & used when we declare a reference. Rather it says “get the memory location value of i’s value.”

So, in all, we are saying “y is a pointer to an int variable, and y points to the memory location of i.”


Okay, fine. So y “points” to i. Thus, the following assignment appears to be legal at first:


y = 5;


But in reality, this is an invalid statement. Keep in mind what y is; y stores the memory location of i. y is not a reference to i. So then how do we use y in order to access i itself? We use the * symbol again to dereference the pointer.


*y = 5;


THIS is legal, and it changes the value of i to 5. The * symbol before the pointer variable (y) tells the program to dereference the pointer. In simpler terms, it tells the program to use the value that is stored at the memory location y is pointing to. 


Constness

Sometimes, we want to have certain variables that should always remain constant. Meaning, these variables should never be able to be changed. We accomplish this by using the const keyword. For example:


const int x = 4;


Reading the declaration, we see that it says “x is an integer that is constant and assign x’s value as 4.” Thus, it is intuitive that, after this assignment, the following would be illegal:


x = 5; //illegal and throws back error because x cannot be changed


Anything that could possibly modify a constant variable is illegal for the program to perform. Notice, we use the word “possibly” since C++ will prevent any operation that MIGHT change a const variable (not just operations that will certainly change them).