1. A pointer can be re-assigned:

    Copyint x = 5;
    int y = 6;
    int *p;
    p = &x;
    p = &y;
    *p = 10;
    assert(x == 5);
    assert(y == 10);
    

    A reference cannot be re-bound, and must be bound at initialization:

    Copyint x = 5;
    int y = 6;
    int &q; // error
    int &r = x;
    
  2. A pointer variable has its own identity: a distinct, visible memory address that can be taken with the unary & operator and a certain amount of space that can be measured with the sizeof operator. Using those operators on a reference returns a value corresponding to whatever the reference is bound to; the reference’s own address and size are invisible. Since the reference assumes the identity of the original variable in this way, it is convenient to think of a reference as another name for the same variable.

    Copyint x = 0;
    int &r = x;
    int *p = &x;
    int *p2 = &r;
    
    assert(p == p2); // &x == &r
    assert(&p != &p2);
    
  3. It is possible to create a pointer to a pointer, but not a pointer to a reference.

    Copy int **pp; // OK, pointer to pointer
     int &*pr; // ill-formed, pointer to reference
    
  4. It is possible to create an array of pointers, but not an array of references.

    Copyint *ap[]; // OK, array of pointers
    int &ar[]; // ill-formed, array of references
    
  5. You can have arbitrarily nested pointers to pointers offering extra levels of indirection. References only offer one level of indirection because references to references collapse.

    Copyint x = 0;
    int y = 0;
    int *p = &x;
    int *q = &y;
    int **pp = &p;
    
    **pp = 2;
    pp = &q; // *pp is now q
    **pp = 4;
    
    assert(y == 4);
    assert(x == 2);
    
  6. A pointer can be assigned nullptr, whereas a reference must be bound to an existing object. If you try hard enough, you can bind a reference to nullptr, but this is undefined and will not behave consistently.

    Copy/* the code below is undefined; your compiler may optimise it
     * differently, emit warnings, or outright refuse to compile it */
    
    int &r = *static_cast<int *>(nullptr);
    
    // prints "null" under GCC 10
    std::cout
        << (&r != nullptr
            ? "not null" : "null")
        << std::endl;
    
    bool f(int &r) { return &r != nullptr; }
    
    // prints "not null" under GCC 10
    std::cout
        << (f(*static_cast<int *>(nullptr))
            ? "not null" : "null")
        << std::endl;
    

    You can, however, have a reference to a pointer whose value is nullptr.

  7. Pointers are ContiguousIterators (of an array). You can use ++ to go to the next item that a pointer is pointing to, and + 4 to go to the 5th element.

  8. A pointer needs to be dereferenced with * to access the object it points to, whereas a reference can be used directly. A pointer to a class/struct uses -> to access its members whereas a reference uses a ..

  9. Const references and rvalue references can be bound to temporaries (see temporary materialization). Pointers cannot (not without some indirection):

    Copyconst int &x = int(12); // legal C++
    int *y = &int(12); // illegal to take the address of a temporary.
    

    This makes const & more convenient to use in argument lists and so forth.

🌐
Educative
educative.io › answers › differences-between-pointers-and-references-in-cpp
Differences between pointers and references in C++
A pointer in C++ is a variable that holds the memory address of another variable. A reference is an alias for an already existing variable.
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › pointers-vs-references-cpp
Pointers vs References in C++ - GeeksforGeeks
2 weeks ago - A reference acts as an alias (another name) for an existing variable. Both are commonly used for parameter passing and efficient data manipulation. A pointer is a variable that stores the memory address of another variable.
Discussions

c++ - What are the differences between a pointer variable and a reference variable? - Stack Overflow
What is the difference between a pointer variable and a reference variable? More on stackoverflow.com
🌐 stackoverflow.com
When to use pointers and references? - C++ Forum
Passing/variables by pointer is faster because it does not make a copy. It also allows the pointer variable to be changed at the root of the function. Passing/variables by reference is similar to pointers but they cannot be reassigned and are aliases instead of memory addresses. More on cplusplus.com
🌐 cplusplus.com
August 30, 2019
c++ - Reference vs. pointer - Stack Overflow
What is the difference? Because this: int Value = 50; int *pValue = &Value; *pValue = 88; and ref version do the same: int Value = 50; int &rValue = Value; rValue = 88; Which one is bet... More on stackoverflow.com
🌐 stackoverflow.com
I am absolutely confused on the topic of references vs pointers
The main problem is that even in the language, a reference is the object, except it only behaves like that in some contexts, in other contexts, it behaves like a pointer. So, kinda, both statements are wrong and correct at the same time. Personally, I do consider the "a reference is a pointer with syntactic sugar" logic more useful. More on reddit.com
🌐 r/cpp
116
94
November 22, 2023
People also ask

When should I use a reference vs a pointer in C++?
Use references when: passing parameters to functions that shouldn't be null, returning values from functions, or creating aliases. Use pointers when: the value might be null (optional parameter), you need to reassign to point to different objects, you're doing dynamic memory allocation, or you need pointer arithmetic.
🌐
cppbetterexplained.com
cppbetterexplained.com › home › posts › c++ reference vs pointer: what's the difference? | cppbe
C++ Reference vs Pointer: What's the Difference? | CppBE
What is the difference between a reference and a pointer in C++?
A reference is an alias for an existing variable — it must be initialized at creation and cannot be reseated to refer to a different variable. A pointer holds the address of a variable, can be null, can be reassigned to point to different objects, and requires * for dereferencing and -&gt; for member access. References are generally safer and cleaner; pointers give more flexibility.
🌐
cppbetterexplained.com
cppbetterexplained.com › home › posts › c++ reference vs pointer: what's the difference? | cppbe
C++ Reference vs Pointer: What's the Difference? | CppBE
Can a C++ reference be null?
No. A C++ reference must be bound to a valid object at initialization and cannot be null. If you need a 'maybe nothing' parameter, use a pointer (which can be nullptr) or std::optional. Creating a null reference through pointer tricks is undefined behavior.
🌐
cppbetterexplained.com
cppbetterexplained.com › home › posts › c++ reference vs pointer: what's the difference? | cppbe
C++ Reference vs Pointer: What's the Difference? | CppBE
🌐
Reddit
reddit.com › r/cpp › i am absolutely confused on the topic of references vs pointers
r/cpp on Reddit: I am absolutely confused on the topic of references vs pointers
November 22, 2023 -

On one hand, there is the Brief Introduction To C++’s Model For Type- And Resource-Safety which is considered to be an authoritative source for the obvious reason that Bjarne Stroustrup himself co-authored it, and it says :

"A reference is a restricted form of a pointer with some added syntactic sugar, so our techniques for pointers also deal with references."

On the other hand, there is the C++ FAQ which is also considered to be an authoritative source, and it says this about references:

"Important note: Even though a reference is often implemented using an address in the underlying assembly language, please do not think of a reference as a funny looking pointer to an object. A reference is the object, just with another name. It is neither a pointer to the object, nor a copy of the object. It is the object. There is no C++ syntax that lets you operate on the reference itself separate from the object to which it refers."

To me, it appears that these statements contradict each other, or do they? I don't know anymore. Could someone enlighten me and for once and for all, define exactly what a reference is, and what's the difference between that and a pointer? Thanks.

Top answer
1 of 5
118
The main problem is that even in the language, a reference is the object, except it only behaves like that in some contexts, in other contexts, it behaves like a pointer. So, kinda, both statements are wrong and correct at the same time. Personally, I do consider the "a reference is a pointer with syntactic sugar" logic more useful.
2 of 5
63
I think the problem you are running into here is that these quotes somewhat muddle semantics with implementation. Let’s separate the two more explicitly, and then why these quotes aren’t actually incompatible or contradictory with each other will become more clear: Pointer’s Semantics A pointer is a distinct value type. It has a defined size on a given platform, which can be obtained by sizeof(void*). On a platform with a true 64 bit address space, this will return 8. All pointers to any object type will have this same size: sizeof(T*) == sizeof(U*). The value of a pointer is an actual, real memory location. You can introspect this directly; if you print a pointer without dereferencing it, the value printed is the memory address value the pointer holds. Basically, pointers are “true” value types in an of themselves and can exist independently of having an object to point to. This is why you can have a null pointer. And since they are true value types, they have their own definition of the following operations, independent of the type they point to: To compare a pointer means to compare the memory address, not the content at that memory address. To copy/assign a pointer means to copy/assign the memory address, not the content at that memory address. Taking the address of a pointer will produce the address of the pointer object itself, not the pointed to content. “Dereferencing” a pointer produces a reference to the pointed to content. I know, it’s confusing. It made more sense in C when there wasn’t a distinct thing called a reference, so pointers were said to have “reference semantics” and thus “dereferencing” them produced the actual content. When we say a type is “pointer like,” we mean it models the properties above. This is why the smart pointers behave the way they do. It’s also why std::span is so confusing. std::span looks like a container, so you expect it to behave the way a reference to a container would: comparing it would compare the elements in the span. But you can reassign spans, and that assignment doesn’t reassign the underlying elements, it just reassigns the underlying pointer to point to new elements. So to be consistent, the standard chose the lesser of two evils and made span model a pointer, and thus has pointer semantics all around. When you compare a span, you’re asking “do these spans provide a view onto literally the same content in memory,” just like comparing a pointer. In C++20 they formalized this “container reference API but pointer semantics” notion with the view concept. Reference’s Semantics A reference is not actually a distinct type, just like that the second quote states: there is no actual object that “represents” a reference. You cannot take the address of the reference itself. If you do sizeof(T&), you will get the same answer as sizeof(T). So most accurately, a reference is an alias for the actual instance of an object. Because references are not true value types in and of themselves, but instead are aliases for the instance, they have the following semantics: They must be “bound” to an instance at initialization. A reference can never be “null.” They cannot be re-assigned. Assigning to a reference after it has been “bound” to an instance is equivalent to assigning to the bound instance, not the reference. Comparing references is equivalent to comparing the referenced instances. Taking the address of a reference gives the address of the bound instance. Implementations As discussed above, a pointer is an explicitly defined value type. They take up memory on the stack/heap. A reference, on the other hand, is not an explicitly defined value type. Because a reference isn’t a “real” value type, but instead defined as an alias to the bound instance, the compiler is free to implement references however it wants, as long as it maintains the semantics. This means a compiler is free to optimize out a reference completely. The compiler cannot inherently do this with a pointer: a pointer has to actually exist because you can take its address, reassign it, etc. However, the compiler is just as free to implement a reference as a pointer (in the machine sense, see below), and often will do so if the reference passes between functions that cannot be inlined. Why is this so confusing? Two reasons: Conflating Pointer with Indirect Referencing When people talk about pointers, they often use them interchangeably with the machine’s notion of a memory address and indirect referencing, because that’s what a (raw) pointer models. But these are actually two distinct things. Pointer the C/C++ type is an abstract notion of a memory address that can be used for indirect referencing. It has meaning independent from how a particular platform implements indirect referencing; there is no “pointer” type in assembly, just instructions that perform operations indirected through a memory address. So when folks say “a reference and a pointer are the same thing” they are thinking in terms of this generated machine assembly for indirect referencing, and how the generated assembly might store the addresses used for it. The “Reference Semantics” Colloquialism There is a common colloquialism to refer both pointers and references as having “reference semantics.” What people mean when they say this is that a given variable name is indirectly representing an instance, without being a value type of that instance. It allows manipulation of the instance through the variable name, and passing the instance between functions without having to copy it. However this colloquialism is a misnomer. A pointer does not, in fact, have reference semantics. It is a value type. It is that value type which models reference semantics, through the * and -> operators. You’ll often hear this pointed out by folks when they ask “does Python/Java have reference semantics or value semantics?” It’s a trick question: the correct answer is “they have value semantics, but the only value type you can directly assign to a name is a pointer.” That said, the lack of a true reference, or an assignable value type other than a pointer, is key to understanding why Java and Python behave the way they do.
🌐
Scaler
scaler.com › home › topics › cpp › pointers vs references in c++
Pointers vs References in C++ - Scaler Topics
June 2, 2022 - This means that a reference to a reference always refers to the same variable as the original reference. Pointers can be assigned a NULL value, which represents an invalid memory address.
Top answer
1 of 16
2247
  1. A pointer can be re-assigned:

    Copyint x = 5;
    int y = 6;
    int *p;
    p = &x;
    p = &y;
    *p = 10;
    assert(x == 5);
    assert(y == 10);
    

    A reference cannot be re-bound, and must be bound at initialization:

    Copyint x = 5;
    int y = 6;
    int &q; // error
    int &r = x;
    
  2. A pointer variable has its own identity: a distinct, visible memory address that can be taken with the unary & operator and a certain amount of space that can be measured with the sizeof operator. Using those operators on a reference returns a value corresponding to whatever the reference is bound to; the reference’s own address and size are invisible. Since the reference assumes the identity of the original variable in this way, it is convenient to think of a reference as another name for the same variable.

    Copyint x = 0;
    int &r = x;
    int *p = &x;
    int *p2 = &r;
    
    assert(p == p2); // &x == &r
    assert(&p != &p2);
    
  3. It is possible to create a pointer to a pointer, but not a pointer to a reference.

    Copy int **pp; // OK, pointer to pointer
     int &*pr; // ill-formed, pointer to reference
    
  4. It is possible to create an array of pointers, but not an array of references.

    Copyint *ap[]; // OK, array of pointers
    int &ar[]; // ill-formed, array of references
    
  5. You can have arbitrarily nested pointers to pointers offering extra levels of indirection. References only offer one level of indirection because references to references collapse.

    Copyint x = 0;
    int y = 0;
    int *p = &x;
    int *q = &y;
    int **pp = &p;
    
    **pp = 2;
    pp = &q; // *pp is now q
    **pp = 4;
    
    assert(y == 4);
    assert(x == 2);
    
  6. A pointer can be assigned nullptr, whereas a reference must be bound to an existing object. If you try hard enough, you can bind a reference to nullptr, but this is undefined and will not behave consistently.

    Copy/* the code below is undefined; your compiler may optimise it
     * differently, emit warnings, or outright refuse to compile it */
    
    int &r = *static_cast<int *>(nullptr);
    
    // prints "null" under GCC 10
    std::cout
        << (&r != nullptr
            ? "not null" : "null")
        << std::endl;
    
    bool f(int &r) { return &r != nullptr; }
    
    // prints "not null" under GCC 10
    std::cout
        << (f(*static_cast<int *>(nullptr))
            ? "not null" : "null")
        << std::endl;
    

    You can, however, have a reference to a pointer whose value is nullptr.

  7. Pointers are ContiguousIterators (of an array). You can use ++ to go to the next item that a pointer is pointing to, and + 4 to go to the 5th element.

  8. A pointer needs to be dereferenced with * to access the object it points to, whereas a reference can be used directly. A pointer to a class/struct uses -> to access its members whereas a reference uses a ..

  9. Const references and rvalue references can be bound to temporaries (see temporary materialization). Pointers cannot (not without some indirection):

    Copyconst int &x = int(12); // legal C++
    int *y = &int(12); // illegal to take the address of a temporary.
    

    This makes const & more convenient to use in argument lists and so forth.

2 of 16
557

What's a C++ reference (for C programmers)

A reference can be thought of as a constant pointer (not to be confused with a pointer to a constant value!) with automatic indirection, ie the compiler will apply the * operator for you.

All references must be initialized with a non-null value or compilation will fail. It's neither possible to get the address of a reference - the address operator will return the address of the referenced value instead - nor is it possible to do arithmetics on references.

C programmers might dislike C++ references as it will no longer be obvious when indirection happens or if an argument gets passed by value or by pointer without looking at function signatures.

C++ programmers might dislike using pointers as they are considered unsafe - although references aren't really any safer than constant pointers except in the most trivial cases - lack the convenience of automatic indirection and carry a different semantic connotation.

Consider the following statement from the C++ FAQ:

Even though a reference is often implemented using an address in the underlying assembly language, please do not think of a reference as a funny looking pointer to an object. A reference is the object. It is not a pointer to the object, nor a copy of the object. It is the object.

But if a reference really were the object, how could there be dangling references? In unmanaged languages, it's impossible for references to be any 'safer' than pointers - there generally just isn't a way to reliably alias values across scope boundaries!

Why I consider C++ references useful

Coming from a C background, C++ references may look like a somewhat silly concept, but one should still use them instead of pointers where possible: Automatic indirection is convenient, and references become especially useful when dealing with RAII - but not because of any perceived safety advantage, but rather because they make writing idiomatic code less awkward.

RAII is one of the central concepts of C++, but it interacts non-trivially with copying semantics. Passing objects by reference avoids these issues as no copying is involved. If references were not present in the language, you'd have to use pointers instead, which are more cumbersome to use, thus violating the language design principle that the best-practice solution should be easier than the alternatives.

🌐
Codefinity
codefinity.com › blog › References-vs-Pointers-in-C-plus-plus
References vs Pointers in C++
Learn about the differences between pointers and references in C++. Pointers store memory addresses and allow for memory manipulation, while references act as aliases for variables, promoting cleaner code.
Find elsewhere
🌐
NTU
www3.ntu.edu.sg › home › ehchua › programming › cpp › cp4_PointerReference.html
C++ Pointers and References
A reference variable provides a new name to an existing variable. It is dereferenced implicitly and does not need the dereferencing operator * to retrieve the value referenced. On the other hand, a pointer variable stores an address. You can change the address value stored in a pointer.
🌐
C++ Better Explained
cppbetterexplained.com › home › posts › c++ reference vs pointer: what's the difference? | cppbe
C++ Reference vs Pointer: What's the Difference? | CppBE
May 16, 2026 - int a = 1, b = 2; int* p = &a; p = &b; // p now points to b — fine int& ref = a; ref = b; // This does NOT rebind ref — it assigns b's VALUE to a // ref still refers to a, and a is now 2 · Pointers require * to dereference and -> for members; references use the same syntax as the original variable:
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › cpp › references-to-pointers
References to pointers | Microsoft Learn
August 3, 2021 - A reference to a pointer is a modifiable value that's used like a normal pointer.
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › pointers-and-references-in-c
Pointers and References in C++ - GeeksforGeeks
2 weeks ago - Pointers store memory addresses and provide indirect access to data. References create an alternative name for an existing variable.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › top differences tutorial › c++ reference vs pointer
C++ Reference vs Pointer | 7 Most Crucial Differences You Must Know
March 18, 2023 - There is a significant difference between C++ reference vs pointer. A reference in C++ is an alternate name for an already existing variable. Once a reference variable is initialized, it can be used to refer to the same variable by another name.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
FavTutor
favtutor.com › blogs › cpp-reference-vs-pointer
Reference vs Pointer in C++: 6 Key Differences to Know
June 26, 2023 - C++ References are considered safer than pointers because of nullability and automatic dereferencing. Since References cannot be null, they can't lead to undefined behavior or memory crashes.
🌐
DEV Community
dev.to › zigrazor › pointer-vs-reference-in-c-the-final-guide-3475
Pointer vs Reference in C++: The Final Guide - DEV Community
September 1, 2021 - A pointer in C++ is a variable that holds the memory address of another variable. A reference is an alias for an already existing variable. Once a reference is initialized to a variable, it cannot be changed to refer to another variable.
🌐
Medium
medium.com › @utkarshbarde2 › understanding-references-vs-pointers-in-c-f6e904e7e78b
Understanding References vs. Pointers in C++ | by Utkarsh Barde | Medium
May 30, 2024 - Initialization: Must be initialized upon declaration. Reassignment: Cannot be reassigned to another variable. Nullability: Cannot be null. Pointers, on the other hand, are variables that hold the memory address of another variable.
🌐
Cplusplus
cplusplus.com › forum › beginner › 261042
When to use pointers and references? - C++ Forum
August 30, 2019 - Do not blow a fuse contemplating it. It's rather simple, and has a purpose, just something more to contemplate (and there's more, but I'll pause here). ... Probably THE biggest difference between a reference and a pointer: A pointer can be null, not pointing to valid memory.
🌐
TutorialsPoint
tutorialspoint.com › pointers-vs-references-in-cplusplus
Pointers vs References in C++
In C++, both pointers and references are used to access and manipulate memory. But they behave differently. This guide explains each with simple words and examples. We understand the topic by learning how each is declared, used, and what differences
🌐
Unstop
unstop.com › home › blog › difference between pointer and reference (+examples)
Difference Between Pointer And Reference (+Examples)
February 4, 2025 - Reference in C++ is created by storing the address of another variable. A reference variable can be considered a constant pointer with automatic indirection, i.e., the compiler will apply an indirection operator (*) for you.
Top answer
1 of 6
20

In this case, they are equivalent.

It does not matter which you use, and neither is "best".

If you really want to choose between them then the reference is probably more idiomatic. I generally stick to references wherever I can because my OCD likes it: they feel "tighter", cannot be re-bound (with or without you noticing) and don't require a dereference to get to the value.

But I'm not aware of any general consensus on the issue for cases such as this.

Also note that the two may not compile to the same code if your implementation does not implement references with pointers, though I know of no implementation like that, and you wouldn't notice the difference anyway.

2 of 6
20

A pointer is the address of the memory location. You can change the value of that address to point at different memory addresses.

A reference is an alias of the variable. You can only assign this alias during declaration. You cannot change which variable the reference is an alias of after it's declared.


The following pointer assignments are not possible with references.

int a = 10;
int b = 20;

int* pInt = NULL; // A pointer pointing at nothing.
pInt = &a; // pInt now points at a
pInt = &b; // pInt now points at b

As for which one is better, it all depends on context.

I use references for method and function parameters.

void updateFoo(Foo& foo)

I use references to alias complex objects.

Foo& foo = bar.getBaz().getFoo(); // easy access to foo

I use pointers for dynamically allocated objects.

Foo* pFoo = new Foo();

I use pointers for things which may point at different values (including no value at all).

Foo* pFoo = NULL;

if (condition1)
    pFoo = &foo1;
else (condition2)
    pFoo = &foo2;

As a general rule, I default to references and use pointers in places where the limitations on references cause problems.

🌐
Aticleworld
aticleworld.com › home › pointers vs references in c++
Pointers vs References in C++ - Aticleworld
August 9, 2022 - Explain difference between pointers and references in C++ (Pointers vs References), that helps you decide when to use pointers or references.
🌐
CPP Scripts
cppscripts.com › reference-vs-pointer-in-cpp
Reference vs Pointer in C++: Key Differences Explained
May 20, 2024 - In C++, a reference is an alias for an existing variable that cannot be changed to refer to another variable, while a pointer is a variable that holds the memory address of another variable and can be reassigned to point to different variables.