C++ Tutorial #20: Introduction to Pointers

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Today I will introduce you to pointers! Before we get started, let me give you a heads up; pointers are very hard to pick up and understand, but once you get the hang of it, you will like pointers, and you will most likely use them when programming your game.

 

 

What are pointers?

 

A Pointer is an object that points to another value stored in memory using its memory address. Think of a variable, for example; int x = 12. We all know in this declaration, int x has a value of 12, but what you might not know is that int x also has an address. In fact, every variable we declare has a memory address and value. Think of the memory address as a home address, a P.O. box, or an IP address; but instead of names and numbers, the memory addresses are represented using hexadecimals.

Here’s an example of a memory address: 0x28ff08

 

 

Okay, that’s cool, but why would we need to use them?

 

When you run into certain parts of your program where you need to access a specific variable, but can’t; you can easily use pointers to “point” to the address and use/change it’s value. Think of it like sending a letter to someone in the mail; In order for a letter to reach to the recipient, the sender has to know the address. Same thing goes with pointers in programming; You have to know the memory address in order to use/change the value. By the end of this tutorial, we will write a program showing the basics of declaring a pointer and pointing it to another variable.

 

 

Let’s learn how to do this!

 

Before we get started on our code, let me show how to declare a pointer, and explain how to use these two new operators we will be using.

 

 

Declaring a Pointer

 

The example below is the syntax for declaring a pointer:

int *varible_name;

 

Here’s another example using the syntax:

 

Just like naming a variable, we can name out pointer as well. Make sure you place an asterisk character (*) in front of the pointer name.

 



 

Address-of Operator (&)

 

To get the address of the variable, you simply just place an ampersand in front of it:

 

 

Dereference Operator (*)

 

The Dereference Operator is used to point the variable and store its address. This can easily be done by placing an asterisk before the pointer and setting a value.

 



 

Begin Coding!

 

Now that you have an idea of how pointers work, let’s write some code! Copy and/or paste this code:

 

 

Here’s what the program should output:

Pointers: Programming Example Output

 

 

How This Code Works

 

We create two variables; var1 and var2. Then we declare a pointer and name it pointer.

 

 

After that, we use the pointer named pointer, to set the pointer value to the address of var1. We use the Dereference Operator to point to var1 and set its value to 10. Continuing with the program, we use the same pointer to point to var2 and set its value to 20.

 

 



 
This section of code is self-explanatory; it prints the values of var1 and var2 we changed by using pointer. The program also prints the address of the two variables. I just added that so you readers know that you can do this as well.

 

Thank you for reading, I hope you have enjoyed this tutorial. Don’t forget to practice your programming, comment, follow me on twitter, like my facebook page, subscribe to my newsletter, and share on social media. We are getting really close to finishing C++, I hope all of you readers get practiced up on your programming. Soon, we will get to the fun stuff when I get to write posts on game design. See you soon!

Facebooktwittergoogle_plusredditpinterestlinkedinmail

About Justin

Computer programmer and owner of kauergames.com.
Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *