Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
C++ Pointer Tutorial

C++ Pointer Tutorial

Because pointers can be ugh

"Regular" variables (not pointers)

To understand a pointer, let's review "regular" variables first. If you're familiar with a programming language without pointers like JavaScript, this is what you think when you hear "variable".

When declaring a variable by identifier (or name), the variable is synonymous with its value.

int number = 3;
std::cout << "number is initialized with a value of " << number << "\n";
// Outputs: number is initialized with a value of 3

The value of the variable can be modified directly.

number++;
number = number * 2;
std::cout << "After modifying number, its value is now " << number << "\n";
// Outputs: After modifying number, its value is now 8

Every value lives at memory addresses. The memory address of a variable can be accessed using the & operator.

std::cout << "The number variable's value lives at memory address " << &number << "\n";

What is a pointer?

A pointer is a variable that holds a memory address where a value lives. [ need a super brief description of why pointers are useful here ]

Declare an empty pointer variable

A pointer is a variable that holds a memory address where a value lives.

A pointer is declared using the * operator before an identifier.

As C++ is a statically typed language, the type is required to declare a pointer. This is the type of data that will live at the memory address it points to.

int *numberPointer;
std::cout << "The numberPointer points to the memory address " << numberPointer << ".\n";
// Outputs: The numberPointer points to the memory address 0x0

We've initialized a pointer, but it points nowhere, it has no memory address. Note that 0x0 is equivalent to 0. It's just the hexadecimal representation of 0, which memory addresses are expressed in.

Set a pointer's memory address

We can set the memory address the pointer points to using the memory address of the number variable.

The memory address of any variable can be accessed using the & operator.

numberPointer = &number;
std::cout << "The numberPointer points to the memory address `" << numberPointer << "`, which is the memory address of number\n";
// Outputs: The numberPointer points to the memory address 0x1001054a0
// NOTE: if you run this code, you will get a different memory address.

Declare a pointer while assigning its address a value

When declaring a pointer, we can alternatively instantiate a variable at its memory address.

int *numberPointer = new int(3);

Instantiating a variable with the new operator always returns a pointer.

Access and modify the value at a pointer's memory address

The value at the memory address pointed to can be accessed with the dereference operator, *.

*numberPointer = 4;
std::cout << "The variable's value has been modified using the pointer. The variable's value is now " << number << "\n";
// Outputs: The variable's value has been modified using the pointer. The variable's value is now 4

The syntactical meaning of the * operator is confusing

We've shown that the asterisk operator * has two semantic meanings: to declare a pointer variable (i.e. int *numberPointer) and also to access the value a pointer points to (*numberPointer = 4). This is confusing but just part of the syntax we need to internalize.

Why use pointers?

In C++, variables are passed to a function by value. When calling a function with an argument, a new variable is instantiated internally and assigned the value passed in the function call.

Any modifications to the value inside the function are performed to this new variable; the variable that was invoked with the function call is unchanged.

void setValueToFive(int x)
{
    std::cout << &x << "\n";
    x = 5;
}

int x = 3;
setValueToFive( x );
std::cout << "The value of x is " << x << ", setValueToFive() did not modify x.\n";
// Outputs: The value of x is 3, setValueToFive() did not modify x

A function can be written to perform the same task but instead accept a pointer as the argument. This lowers the memory footprint of the program. Unnecessary duplicate variables aren't created. The function can modify the variable's value directly. Any modifications to the variable in the function affect the variable here in this scope too.

void setPointerValueToFive(int *x)
{
    *x = 5;
}

int x = 3;
setPointerValueToFive( &x );
std::cout << "The value of x is " << x << ", setPointerValueToFive() modified x.\n";
// Outputs: The value of x is 5, setPointerValueToFive() modified x

In C++ (not in C), you can pass a variable by reference by passing in a variable, and defining the function's parameter like int &x. Any modifications to the variable inside the function will also affect the variable here in this scope too.

void setValueToFiveWithReference(int& x)
{
    x = 5;
}

int x = 3;
setValueToFiveWithReference( x );
std::cout << "The value of x is " << x << ", setPointerValueToFive() modified x.\n";
// Outputs: The value of x is 5, setPointerValueToFive() modified x

Which to use is a matter of style, and whether you want to perform pointer arithmetic in the function. Google has specific guidelines for which to use where.

The pointer is a variable in itself, and all variables are stored somewhere in memory.

We can see the memory address the pointer's memory address is stored.

@lzzy12

This comment has been minimized.

Copy link

lzzy12 commented Jan 23, 2018

Wow! Now this cleared all my doubts about pointers. Thankkkkks sooooo much for this awesome gist.

@NightfallAlicorn

This comment has been minimized.

Copy link

NightfallAlicorn commented Jan 27, 2018

It really feels strange coming from JavaScript to trying out C++. I picked most stuff up easily but I was confused what this & and * was all about even after skimming tutorials. This has helped enlighten me, thanks.

@bmcdonnell

This comment has been minimized.

Copy link

bmcdonnell commented Feb 3, 2018

We've shown that the asterisk operator * has two semantic meanings: to declare a pointer variable (i.e. int *numberPointer) and also to access the value a pointer points to (*numberPointer = 4). This is confusing but just part of the syntax we need to internalize.

I think there is another way to conceptualize the declaration which unifies the semantic meanings - to an extent. If I do int var;, I say "var is an int". If I do int *ptr;, I can say "*var is an int", prounounced as "dereferenced-var is an int".

@vardanator

This comment has been minimized.

Copy link

vardanator commented Feb 10, 2018

If someone needs an illustrative approach, take a look at this tutorial: https://medium.com/@vardanator/self-organizing-sorted-linked-list-in-c-ae24b7e0b600

@RudyFiero

This comment has been minimized.

Copy link

RudyFiero commented Feb 13, 2018

Bad link for the following.

Google has specific guidelines for which to use where.
http://google-styleguide.googlecode.com/svn/trunk/cppguide.html#Reference_Arguments

  1. That’s an error.
    The requested URL /svn/trunk/cppguide.html was not found on this server. That’s all we know.

But thanks for this. It helped.

@mariobro

This comment has been minimized.

Copy link

mariobro commented Feb 14, 2018

In response to the above comment - the new link is at https://google.github.io/styleguide/cppguide.html#Reference_Arguments now it seems!

@fmpasquali

This comment has been minimized.

Copy link

fmpasquali commented Feb 23, 2018

Awesome explanation! Thanks

@iErcann

This comment has been minimized.

Copy link

iErcann commented Mar 1, 2018

Thank you !

@cy-xu

This comment has been minimized.

Copy link

cy-xu commented Apr 6, 2018

Very clear, thank you!

@chiefGui

This comment has been minimized.

Copy link

chiefGui commented Apr 25, 2018

Thank you! Was looking for something this clear.

@iAnanich

This comment has been minimized.

Copy link

iAnanich commented Apr 29, 2018

Thank you!
And I want to propose a little edit: on the 73 line, wrap new as code, for example, for better readability, yo help people understand that you are talking about the specific operator.

@marisancans

This comment has been minimized.

Copy link

marisancans commented May 28, 2018

Been searching for some easy to understand example and finally found one, thanks!

@JeroenVranken

This comment has been minimized.

Copy link

JeroenVranken commented May 29, 2018

Thanks! Clearing up the duplicate syntactical meaning was really helpful!

@bcverdict

This comment has been minimized.

Copy link

bcverdict commented Jul 29, 2018

More than helpful and very easy to understand. Thank you for this!

@chris-schmitz

This comment has been minimized.

Copy link

chris-schmitz commented Oct 2, 2018

Def a nice/quick explanation of the concept. I'd been looking around for a quick explanation and found answers with overly explained (for my purposes) answers. This hit all of the notes needed perfectly. Thanks!

@AKhalifa0101

This comment has been minimized.

Copy link

AKhalifa0101 commented Nov 20, 2018

Thank You very much
It is so exciting and easy to understand .

@olotieno

This comment has been minimized.

Copy link

olotieno commented May 20, 2019

Thank you..

@hinell

This comment has been minimized.

Copy link

hinell commented May 26, 2019

Thanks for sharing.

@NightfallAlicorn Coming from C++ to JavaScript is much harder for sure than otherwise. I also got everything quickly after learning JavaScript and TypeScript. Pointers don't seem to me harder either though. I don't even find x86-64 assembler harder than all these knowing how things work inside.

@ghost

This comment has been minimized.

Copy link

ghost commented May 29, 2019

Nice tutorial.
What I don't like when I see, everyone using the * and & wherever they want. It's not flexible but more confusing.
int *x; int* x; int &x; int& x;

@sabahat278

This comment has been minimized.

Copy link

sabahat278 commented Jul 13, 2019

Nice and easy explanation of pointers
https://youtu.be/f7p190xYTxo

@pankaj-pundir

This comment has been minimized.

Copy link

pankaj-pundir commented Aug 1, 2019

This guide just cleared all doubts. Thanks man..

@netnic1986

This comment has been minimized.

Copy link

netnic1986 commented Aug 11, 2019

Pointers in c++-: Pointer is one of the key aspects of c++ language similar to that of c programming. Pointer offers a unique approach to handle data in c and c++ language.

We know that a pointer is a derived data type that refers to another data variable by storing the variable memory address rather than data. a pointer variable define where to get the value of a specific data variable instead of defining actual data .

Like the c programming language variable can also refer another pointer in c++. However it often point to a data variable . Pointer also provide an alternative approach to access other data object.

Defining a pointer in c++ -: Like any other programming language before we use the pointer in c++ program it is necessary to defining. We can declare a pointer variable similar to the variable in c++. Like c language variable . The declaration is based on the data type of the variable it point to the declaration of a pointer variable takes like this

data-type * pointer-variable;
In this example the pointer variable is the name of the pointer and data type refer to the one of the valid c++ data type such as int,char,float and so many more. The data type is followed by an asterisk (*) which distinguishes a pointer variable from other variable to the computer.

As we know a pointer variable can point to any type of data available in c++. It is necessary to understand that a pointer is able to point to only one data type at the specific time. Let us declare a pointer variable which points to an integer variable as like

int *ptr ;
here ptr is a pointer variable and point to an integer data type. the pointer variable ptr should contain memory location of any integer variable. in the same manner we can declare pointer variable for other data type also.
more about pointer

@aharisingh

This comment has been minimized.

Copy link

aharisingh commented Sep 15, 2019

Really great explanation for pointers.Thanks a lot.

@Fabhi

This comment has been minimized.

Copy link

Fabhi commented Sep 18, 2019

We've shown that the asterisk operator * has two semantic meanings: to declare a pointer variable (i.e. int *numberPointer) and also to access the value a pointer points to (*numberPointer = 4). This is confusing but just part of the syntax we need to internalize.

I think there is another way to conceptualize the declaration which unifies the semantic meanings - to an extent. If I do int var;, I say "var is an int". If I do int *ptr;, I can say "*var is an int", pronounced as "dereferenced-var is an int".

I think it should be "dereferenced-ptr". But great job!

@dekhoroom

This comment has been minimized.

Copy link

dekhoroom commented Sep 25, 2019

such a best explained www.dekhoroom.com

@runBAMrunfaster

This comment has been minimized.

Copy link

runBAMrunfaster commented Sep 29, 2019

As a C++ newbie, I found this to be a super useful explanation of a concept I was having a real problem with wrapping my head around. Pointers can be, as you say, "ugh."

@KaiWangCanada

This comment has been minimized.

Copy link

KaiWangCanada commented Oct 9, 2019

I don't always understand pointers. But when I understand, it is through github.

@Aivi001

This comment has been minimized.

Copy link

Aivi001 commented Oct 21, 2019

Thank you for the concise and clear explanation!! It served as a really quick and effective brush-up of the concept. Also, thx for the Google guidlines link, I didn't know about all that until you mentioned it here :) Stay blessed!

@benvision1

This comment has been minimized.

Copy link

benvision1 commented Dec 9, 2019

Thanks a lot

@jefflgaol

This comment has been minimized.

Copy link

jefflgaol commented Dec 10, 2019

Thank you!

@HermesTrismegistus

This comment has been minimized.

Copy link

HermesTrismegistus commented Jan 13, 2020

I signed in to complement you. Been learning c++ for couple of month now. Already used pointers a lot, had some idea's about it from doing tutorials etc. But this is the first time i read something about pointers, and thought ah now i get it! Thanx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.