Skip to content

Instantly share code, notes, and snippets.

@ericandrewlewis
Last active June 6, 2024 01:43
Show Gist options
  • Save ericandrewlewis/720c374c29bbafadedc9 to your computer and use it in GitHub Desktop.
Save ericandrewlewis/720c374c29bbafadedc9 to your computer and use it in GitHub Desktop.
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.

@mariobro
Copy link

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

@fmpasquali
Copy link

Awesome explanation! Thanks

@iErcann
Copy link

iErcann commented Mar 1, 2018

Thank you !

@cy-xu
Copy link

cy-xu commented Apr 6, 2018

Very clear, thank you!

@chiefGui
Copy link

Thank you! Was looking for something this clear.

@iAnanich
Copy link

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
Copy link

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

@JeroenVranken
Copy link

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

@bcverdict
Copy link

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

@chris-schmitz
Copy link

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
Copy link

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

@olotieno
Copy link

Thank you..

@hinell
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.

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
Copy link

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

@pankaj-pundir
Copy link

This guide just cleared all doubts. Thanks man..

@netnic1986
Copy link

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
Copy link

Really great explanation for pointers.Thanks a lot.

@Fabhi
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
Copy link

such a best explained www.dekhoroom.com

@runBAMrunfaster
Copy link

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
Copy link

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

@Aivi001
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
Copy link

Thanks a lot

@jefflgaol
Copy link

Thank you!

@EsoCoding
Copy link

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

@shammo84
Copy link

Thank you so much, finally clear after 4 years lol.

@Haytham-Farouk
Copy link

Thanks a lot and highly appreciated.
Just a quick question please regarding the phrase...
"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."
How about using a global variable, and working on it inside the function, it is the same thing isn't it?
Thanks again.

@lovely-error
Copy link

How does this feature interact with various move semantics? Explanation of T&& will be appreciated. 😃

@YacineDeghaies
Copy link

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".

You mean "*ptr is an int"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment