Introduction
In the previous tutorial, we discussed classes. Classes are a great way to group related variables and methods together. Also, they make for a logical division of code into multiple files, so that you don’t have thousands of lines of code in one file. At the end of the tutorial, I alluded to a better way to pass a variable into a method, modify it and return the modified value. This is going to lead us to the topic of pointers, which is a very powerful tool and an essential one if you want to master C++.
Code
Pointers are basically an address in memory. (They are called pointers because they point to where your data is located in memory.) So, how do we get the address of a variable? Here’s how:
int num = 5;
cout << &num << endl;
We declare a variable and then we use the & symbol before the variable to get the address of the variable. This works with any data type, even if the variable is a struct or a class.
So, a pointer is a way to store that address:
int* ptr = #
cout << ptr << endl;
So now the ptr variable stores the location of the num variable. The value printed to the screen from both code snippets is the same. When declaring a pointer, you need to tell the compiler what data type it is pointing to. In this case, we are pointing to an integer. The star between the int and the variable name says that you want a pointer to an integer (instead of storing the integer itself). One interesting thing to note is that since the compiler ignores whitespace, the following are exactly the same:
int *ptr1 = #
int* ptr2 = #
int*ptr3 = #
Now, if you want to get the value that the pointer is pointing to, you do this:
int val = *ptr;
cout << val << endl;
The asterisk before the pointer variable tells the computer that you want the value that the pointer is pointing to. Therefore, the & and the * operators are inverses of each other.
Now, let’s take a step back and diagram what is happening in memory:
Variable Name | Address | Value |
<other_stuff> | <other_stuff> | <other_stuff> |
ptr | 001BFA7C | 001BFA88 |
<other_stuff> | <other_stuff> | <other_stuff> |
num | 001BFA88 | 5 |
<other_stuff> | <other_stuff> | <other_stuff> |
So, the ptr variable stores the location of the num variable as its value, and since the ptr variable is something that the computer has to store, that means that it has a location in memory as well. So, wait does this mean that you can do something like this?
int** ptr2 = &ptr;
You sure can! This means that ptr2 is a pointer, which has a value in memory is the address of a pointer. So, in our example above the value of ptr2 would be 001BFA7C. Furthermore, you can get back the original value of num by doing something like this:
cout << **ptr2 << endl;
This is some extremely powerful stuff and it can get pretty confusing trying to keep what points to what straight.
One use for pointers is to pass a value to a function so that it can be modified. Take this code snippet for example:
void fun(int num)
{
num = 7;
}
int main()
{
int num = 5;
fun(num);
cout << num << endl;
return 0;
}
What value do you think is printed out? The answer is 5. The reason is that when you call a function, by default it copies the values that you pass in and the function works on the copies. So, in this case, the copy’s value is changed to 7. Well, this doesn’t change the value of num in main, so it remains 5 the whole time. One way to demonstrate this is to print out the location of the variables in memory, using the & operator:
void fun(int num)
{
num = 7;
cout << &num << endl;
}
int main()
{
int num = 5;
cout << &num << endl;
fun(num);
cout << num << endl;
return 0;
}
If you run this, then you will clearly see that the num in fun and the num in main point to two different locations in memory. So, how can we modify this so that they point to the same location in memory? How about something like this:
void fun(int* ptr)
{
*ptr = 7;
}
int main()
{
int num = 5;
int* ptr = #
fun(ptr);
cout << num << endl;
return 0;
}
Now we pass in a pointer, which points to the variable num. The pointer is copied (much like the int was copied), but this really doesn’t matter to us. We don’t care about the location of the pointer in memory, all we care about is what it points to (which is num). So, we can use the * operator to get at the value of num and change it. Here’s what the computer’s memory looks like:
Variable Name | Address | Value |
<other_stuff> | <other_stuff> | <other_stuff> |
num | 0024F894 | 5 |
<other_stuff> | <other_stuff> | <other_stuff> |
ptr (in main) | 0024F974 | 0024F894 |
<other_stuff> | <other_stuff> | <other_stuff> |
ptr (in fun) | 0024F980 | 0024F894 |
<other_stuff> | <other_stuff> | <other_stuff> |
At first glance, the code above might seem a bit weird because we are using the * operator on the left hand side of the equals sign. This is a nice feature that lets us set the value the pointer is pointing to.
Since this is a fairy common operation, there is a short hand version of it that looks something like this:
void fun(int& num)
{
num = 7;
}
int main()
{
int num = 5;
fun(num);
cout << num << endl;
return 0;
}
This tells the compiler that you do not want it to copy the value that you are passing in, that you want to manipulate the value directly. If you print out the address of num within fun and main like we did before, you will see that they are the same location in memory.
That’s the trick that we could have used in the previous tutorial so that we didn’t have to pass the Fruit class back as the return parameter.
Summary
In this tutorial, we started to learn about pointers and how they store the location of another variable in memory.
In the next tutorial, we will build upon this knowledge by dynamically allocating memory for storing arrays. If you think back to the tutorial on arrays, we knew the size of the array when we were writing the program. This is usually not the case, so in the next tutorial, we will talk about how to make an array of an arbitrary size.
If you have any questions or comments about what was covered here, post them to the comments. I watch them closely and will respond and try to help you out.