6.7 — Introduction to pointers

In lesson 1.3 -- a first look at variables, we noted that a variable is a name for a piece of memory that holds a value. When our program instantiates a variable, a free memory address is automatically assigned to the variable, and any value we assign to the variable is stored in this memory address.

For example:

When this statement is executed by the CPU, a piece of memory from RAM will be set aside. For the sake of example, let’s say that the variable x is assigned memory location 140. Whenever the program sees the variable x in an expression or statement, it knows that it should look in memory location 140 to get the value.

The nice thing about variables is that we don’t need to worry about what specific memory address is assigned. We just refer to the variable by its given identifier, and the compiler translates this name into the appropriately assigned memory address.

However, this approach has some limitations, which we’ll discuss in this and future lessons.

The address-of operator (&)

The address-of operator (&) allows us to see what memory address is assigned to a variable. This is pretty straightforward:

On the author’s machine, the above program printed:


Note: Although the address-of operator looks just like the bitwise-and operator, you can distinguish them because the address-of operator is unary, whereas the bitwise-and operator is binary.

The dereference operator (*)

Getting the address of a variable isn’t very useful by itself.

The dereference operator (*) allows us to access the value at a particular address:

On the author’s machine, the above program printed:


Note: Although the dereference operator looks just like the multiplication operator, you can distinguish them because the dereference operator is unary, whereas the multiplication operator is binary.


With the address-of operator and dereference operators now added to our toolkits, we can now talk about pointers. A pointer is a variable that holds a memory address as its value.

Pointers are typically seen as one of the most confusing parts of the C++ language, but they’re surprisingly simple when explained properly.

Declaring a pointer

Pointer variables are declared just like normal variables, only with an asterisk between the data type and the variable name.

Syntactically, C++ will accept the asterisk next to the data type, next to the variable name, or even in the middle. Note that this asterisk is not a dereference. It is part of the pointer declaration syntax.

However, when declaring multiple pointer variables, the asterisk has to be included with each variable. It’s easy to forget to do this if you get used to attaching the asterisk to the type instead of the variable name!

For this reason, when declaring a variable, we recommend putting the asterisk next to the variable name.

Best practice: When declaring a pointer variable, put the asterisk next to the variable name.

However, when returning a pointer from a function, it’s clearer to put the asterisk next to the return type:

This makes it clear that the function is returning a value of type int* and not an int.

Best practice: When declaring a function, put the asterisk of a pointer return value next to the type.

Just like normal variables, pointers are not initialized when declared. If not initialized with a value, they will contain garbage.

Assigning a value to a pointer

Since pointers only hold addresses, when we assign a value to a pointer, that value has to be an address. One of the most common things to do with pointers is have them hold the address of a different variable.

To get the address of a variable, we use the address-of operator:

Conceptually, you can think of the above snippet like this:

This is where pointers get their name from -- ptr is holding the address of variable value, so we say that ptr is “pointing to” value.

It is also easy to see using code:

On the author’s machine, this printed:


The type of the pointer has to match the type of the variable being pointed to:

Note that the following is also not legal:

This is because pointers can only hold addresses, and the integer literal 5 does not have a memory address. If you try this, the compiler will tell you it cannot convert an integer to an integer pointer.

C++ will also not allow you to directly assign literal memory addresses to a pointer:

The address-of operator returns a pointer

It’s worth noting that the address-of operator (&) doesn’t return the address of its operand as a literal. Instead, it returns a pointer containing the address of the operand, whose type is derived from the argument (e.g. taking the address of an int will return the address in an int pointer).

We can see this in the following example:

On Visual Studio 2013, this printed:

int *

(With gcc, this prints “pi” (pointer to integer) instead).

This pointer can then be printed or assigned as desired.

Dereferencing pointers

Once we have a pointer variable pointing at something, the other common thing to do with it is dereference the pointer to get the value of what it’s pointing at. A dereferenced pointer evaluates to the contents of the address it is pointing to.

The above program prints:


This is why pointers must have a type. Without a type, a pointer wouldn’t know how to interpret the contents it was pointing to when it was dereferenced. It’s also why the type of the pointer and the variable address it’s being assigned to must match. If they did not, when the pointer was dereferenced, it would misinterpret the bits as a different type.

Once assigned, a pointer value can be reassigned to another value:

When the address of variable value is assigned to ptr, the following are true:

  • ptr is the same as &value
  • *ptr is treated the same as value

Because *ptr is treated the same as value, you can assign values to it just as if it were variable value! The following program prints 7:

A warning about dereferencing invalid pointers

Pointers in C++ are inherently unsafe, and improper pointer usage is one of the best ways to crash your application.

When a pointer is dereferenced, the application attempts to go to the memory location that is stored in the pointer and retrieve the contents of memory. For security reasons, modern operating systems sandbox applications to prevent them from improperly interacting with other applications, and to protect the stability of the operating system itself. If an application tries to access a memory location not allocated to it by the operating system, the operating system may shut down the application.

The following program illustrates this, and will probably crash when you run it (go ahead, try it, you won’t harm your machine):

The size of pointers

The size of a pointer is dependent upon the architecture the executable is compiled for -- a 32-bit executable uses 32-bit memory addresses -- consequently, a pointer on a 32-bit machine is 32 bits (4 bytes). With a 64-bit executable, a pointer would be 64 bits (8 bytes). Note that this is true regardless of what is being pointed to:

As you can see, the size of the pointer is always the same. This is because a pointer is just a memory address, and the number of bits needed to access a memory address on a given machine is always constant.

What good are pointers?

At this point, pointers may seem a little silly, academic, or obtuse. Why use a pointer if we can just use the original variable?

It turns out that pointers are useful in many different cases:

1) Arrays are implemented using pointers. Pointers can be used to iterate through an array (as an alternative to array indices) (covered in lesson 6.8).
2) They are the only way you can dynamically allocate memory in C++ (covered in lesson 6.9). This is by far the most common use case for pointers.
3) They can be used to pass a large amount of data to a function in a way that doesn’t involve copying the data, which is inefficient (covered in lesson 7.4)
4) They can be used to pass a function as a parameter to another function (covered in lesson 7.8).
5) They can be used to achieve polymorphism when dealing with inheritance (covered in lesson 12.1).
6) They can be used to have one struct/class point at another struct/class, to form a chain. This is useful in some more advanced data structures, such as linked lists and trees.

So there are actually a surprising number of uses for pointers. But don’t worry if you don’t understand what most of these are yet. Now that you understand what pointers are at a basic level, we can start taking an in-depth look at the various cases in which they’re useful, which we’ll do in subsequent lessons.


Pointers are variables that hold a memory address. They can be dereferenced using the dereference operator (*) to retrieve the value at the address they are holding. Dereferencing a garbage pointer may crash your application.

Best practice: When declaring a pointer variable, put the asterisk next to the variable name.
Best practice: When declaring a function, put the asterisk of a pointer return value next to the type.


1) What values does this program print? Assume a short is 2 bytes, and a 32-bit machine.

Show Solution

2) What’s wrong with this snippet of code?

Show Solution

6.7a -- Null pointers
6.6 -- C-style strings

150 comments to 6.7 — Introduction to pointers

  • Peter Baum

    This comment is about editing comments: if you have

    in the comment and then edit the comment, those tags don't seem to work anymore.  My workaround was to copy the comment, delete the comment, and then create a new comment where I can paste the old edited comment.

    In fact, that is how I got those tags to show properly in this comment!

  • Peter Baum

    In the section "Assigning a value to a pointer" I think it might be helpful to go over the various combinations of ordinary variable names and the * and & operators. Consider the following code and lines 7 through 10 in particular.  I suggest an explanation of why each of those lines works or does not work.

  • Peter Baum

    In section "Assigning a value to a pointer" - in the sentence

    "One of the most common things to do with pointers is have them hold the address of a different variable."

    I did not understand why the word "different" was used.  Wouldn't it be very rare to have an application that required a pointer to point to itself?  In any case, an example of such an application might be interesting and helpful.

    • nascardriver

      Hi Peter!

      > an example of such an application
      I doubt the existence of such an application.

      • Peter Baum

        Yes, I struggled with it as well, which is why I wondered why "different" was in there at all.  The only thing I could think of was that doing so might be used to represent a null pointer that would always guaranteed that the address was accessible.

        • nascardriver

          That's what nullptr is for (Lesson 6.7a)

          • Peter Baum

            I had not read the next lesson yet, so I went there at your good suggestion.  However, I didn't see where Alex explained how nullptr was implemented. That would be both helpful and interesting.  

            I'm the kind of person who likes to know how things work.  Another example is with the descriptions about scope.  Rather than just memorizing a bunch of rules, it is easier for me to learn if I understand what the compiler and linker are actually doing.

            • nascardriver

              I am worried there's not much to research about it's implementation.

              Quote from
              "Even though nullptr_t it is not a keyword, it identifies a distinct fundamental type"

  • david

    Hi Alex,  
    is there a convention for naming pointers?
    especially those that point to dynamically allocated memory.
    what would be an appropriate prefix or suffix?

    • nascardriver

      Hi David!

      There any many naming conventions.
      I like hungarian notation (Modified to my liking) where pointers start with a lower case 'p' followed by the actual name.

      Here's a table of 'normal' hungarian notation:

    • Alex

      There isn't a standard prefix for these kinds of things. As Nascardriver points out, some users use a "p" prefix or "Ptr" suffix to denote that a variable is a pointer. This doesn't tell you whether the pointer is pointing to dynamically allocated memory or not though.

      Personally, I don't think adding the prefix or suffix is valuable, since the strong typing system will rarely let you interchange a pointer and non-pointer value. Better to focus on making your variable names actually good.

  • PLZ ALEX reply this .

    as per explanation of quiz question 2 in 6.7 introduction to pointers

    int value = 5;
    int *ptr = &value; // ptr points to value

    *ptr = 7; // *ptr is the same as value, which is assigned 7(HOW A '7' CAN BE ASSIGNED TO '5' AS *ptr RETRIEVES THE VALUE OF VARIABLE "value" i.e. 5)
    std::cout << value; // prints 7

    the  3rd line also should not compile as [quiz 2nd question solution]:
    the asterisk here represents a dereference, which is used to get the value that a pointer is pointing to. So this line says, “retrieve the value that ptr is pointing to (an integer here 5), and overwrite it with the   literal  7). That doesn’t make any sense -- you can’t assigna literal to an integer literal!

    • nascardriver

      Hi prince GUPTA!

    • Alex

      Nascardriver's answer is correct. Just to be clear, *ptr is the same as value. We can use value in two contexts: as an l-value (e.g. value = 5) or as an r-value to be evaluated to produce a value (std::cout << value). *ptr works identically. We can use it as an l-value (*ptr = 7) or an r-value (std::cout << *ptr).

  • D.Master

    pointers don't store values [expletive removed]

    • Alex

      From wikipedia (

      "... a pointer is a programming language object, whose value refers to (or "points to") another value stored elsewhere in the computer memory using its memory address".

      Seems pretty clear that pointers have values, those values are just addresses.

  • hey alex, this question is a bit off topic. I hope you won't mind.
    So, I am having problem with this code

    using namespace std;
    int main()
    char ch = 'a';

    when I ran it, it gave  "a )"
    what does this mean?"ch" does not take a valid address in memory?

    • Alex

      std::cout treats objects of type char* as C-style strings, and will print characters until it hits a null terminator.

      You've essentially told the compiler to print the character 'a' as a string -- so you get the 'a' as the first character, and then everything else beyond that is junk in memory.

    • D.Master

      This question on address of a char has been a great aid to me in my learning of c++. Std::strings can be iniatialised  with char pointers. Pointers even find use in other situation to mean a set of elements. Eg: sf::Text *text is an array of sf::Text elements. Because pointer is not smart you need to specify a size type for the number of elements. In the case of char pointers, the last element is a NULL value of 0

Leave a Comment

Put all code inside code tags: [code]your code here[/code]