Your function expects a reference to an actual string pointer in the calling scope, not an anonymous string pointer. Thus:

Copystring s;
string* _s = &s;
myfunc(_s);

should compile just fine.

However, this is only useful if you intend to modify the pointer you pass to the function. If you intend to modify the string itself you should use a reference to the string as Sake suggested. With that in mind it should be more obvious why the compiler complains about you original code. In your code the pointer is created 'on the fly', modifying that pointer would have no consequence and that is not what is intended. The idea of a reference (vs. a pointer) is that a reference always points to an actual object.

Answer from Chris on Stack Overflow
🌐
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.
Discussions

c++ - Difference between pointer to a reference and reference to a pointer - Stack Overflow
What is the difference between pointer to a reference, reference to a pointer and pointer to a pointer in C++? Where should one be preferred over the other? 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
Can I take a reference of a pointer in C++? - Stack Overflow
I am passing in a reference to a pointer variable into a function. The function will do something and point the pointer variable to some object. Code: int Foo(Obj* &ptr) { // do something... ... More on stackoverflow.com
🌐 stackoverflow.com
c++ - What is a reference-to-pointer? - Stack Overflow
I recently saw a function that is being declared as: void func(type* ¶m); I already know the difference between type* param and type& param. How does the above differ from them? And wh... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › passing-reference-to-a-pointer-in-c
Passing Reference to a Pointer in C++ - GeeksforGeeks
July 11, 2025 - Note: It is allowed to use "pointer to pointer" in both C and C++, but we can use "Reference to pointer" only in C++.
🌐
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.
🌐
NTU
www3.ntu.edu.sg › home › ehchua › programming › cpp › cp4_PointerReference.html
C++ Pointers and References
It shall be read as "newName is a reference to existingName", or "newNew is an alias of existingName". You can now refer to the variable as newName or existingName. ... A reference works as a pointer. A reference is declared as an alias of a variable.
Top answer
1 of 6
78

First, a reference to a pointer is like a reference to any other variable:

Copyvoid fun(int*& ref_to_ptr)
{
    ref_to_ptr = 0; // set the "passed" pointer to 0
    // if the pointer is not passed by ref,
    // then only the copy(parameter) you received is set to 0,
    // but the original pointer(outside the function) is not affected.
}

A pointer to reference is illegal in C++, because -unlike a pointer- a reference is just a concept that allows the programmer to make aliases of something else. A pointer is a place in memory that has the address of something else, but a reference is NOT.

Now the last point might not be crystal clear, if you insist on dealing with references as pointers. e.g.:

Copyint x;
int& rx = x; // from now on, rx is just like x.
// Unlike pointers, refs are not real objects in memory.
int* p = &x; // Ok
int* pr = ℞ // OK! but remember that rx is just x!
// i.e. rx is not something that exists alone, it has to refer to something else.
if( p == pr ) // true!
{ ... }

As you can see from the above code, when we use the reference, we are not dealing with something separated from what it refers to. So, the address of a reference is just the address of what it refers to. Thats why there is no such thing called the address of the reference in terms of what you are talking about.

2 of 6
65

Pointer to a pointer

A pointer in C++ is just a value which stores a memory location (generally as a 32-bit value).

Let's say you had a user input integer value (78 == 0x4E in hex).

It would be stored in memory in a similar way to this (I'm purposely simplifying things for this example):

CopyMemory address    Value
0x12345678        0x0000004E

If you wanted to create a "pointer" to this value, it would look like this in memory:

CopyMemory address    Value
0x22334455        0x12345678

At memory address 0x22334455 you now have a "pointer" whose value is 0x12345678, or the memory address of where the user input integer value (0x4E) is stored.

Let's say you wanted to create a "pointer" to this pointer value. It would look like this:

CopyMemory address    Value
0x11335577        0x22334455

You now have a new "pointer" value in memory which is storing the memory address of the previously-defined pointer value.

Pointers can be created like this indefinitely - the key is remembering that a pointer is just another value that the compiler interprets as a memory location (and it provides various access semantics such as * and -> which are special to "pointer" types).

Reference to a pointer

A reference can be thought of as a view, or alias, on to another real object. When you create a reference to a pointer called myReference, you are simply defining a new name called myReference which can be used to access the pointer which you have previous defined in memory.

Internally, references are implemented using pointers, but this is beyond the scope of your question.

References have restrictions over other types in C++ - for example, you must always initialize a reference to "refer" to a real object when you create it, while a pointer may point to memory which is invalid, or uninitialised.

Pointer to a reference

This doesn't exist. As stated earlier, a reference is merely an alias to another object. You can't "point" to a reference, because it isn't an object in itself but merely another name for a real object.

Of course, you can have a pointer to the object that a reference is referring to. But now we are back in vanilla pointer territory.

Note about parameters

When you pass a parameter by value to a method or routine, you are essentially passing a "copy" of the object to the method. Any changes you make to the value within the routine will be lost when the routine returns, because the parameter will be treated as a local variable in the context of the routine.

If you want to modify a parameter which is passed in so the client (calling) code can access the change, you must pass the parameter by pointer or by reference.

For example:

Copyvoid myMethod(int myValue)
{
    // NOTE: This change will be lost to the caller!
    myValue = 5;
}

void myMethod2(int* myValue)
{
    // Correct way of modifying pointer parameter value
    *myValue = 5;
}

void myMethod3(int& myValue)
{
    // Correct way of modifying reference parameter value
    myValue = 5;
}

Let's now say that your method wants to allocate memory for a pointer. You could be tempted to do this:

Copyvoid myMethod4(int* myValue)
{
    // Warning: You will lose the address of the allocated
    // memory when you return!
    myValue = new int[5];
}

But remember, you are modifying the copy of the pointer value here, not the real pointer value. Since you are wanting to modify the pointer in this routine, and not the value that the pointer "points" to, you need to pass it in as a "pointer to a pointer" or a "reference to a pointer":

Copyvoid myMethod5(int** myValue)
{
    // Correct way of allocating memory in a method
    // via pointer-to-pointer
    *myValue = new int[5];
}

void myMethod6(int*& myValue)
{
    // Correct way of allocating memory in a method
    // via reference-to-pointer
    myValue = new int[5];
}

In these bottom 2 examples, the code which is calling myMethod5 and myMethod6 will correctly get the memory address of the newly-allocated memory via the myValue parameter pointer or reference.

🌐
GeeksforGeeks
geeksforgeeks.org › c++ › pointers-vs-references-cpp
Pointers vs References in C++ - GeeksforGeeks
3 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.
Find elsewhere
🌐
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.
🌐
CodeProject
codeproject.com › articles › Pointer-to-Pointer-and-Reference-to-Pointer
Pointer to Pointer and Reference to Pointer - CodeProject
Explains the reason behind using pointer-to-pointer and reference-to-pointer to modify a pointer passed to a function
🌐
cppreference.com
en.cppreference.com › book › pointers
Pointers - cppreference.com
December 19, 2013 - In general, pointer is a type of a variable that stores a link to another object. In C and C++, the link is the address of that object in the program memory. Pointers allow to refer to the same object from multiple locations of the source code without copying the object.
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › pointers-and-references-in-c
Pointers and References in C++ - GeeksforGeeks
3 weeks ago - Dereferencing the pointer using *myptr accesses the value stored at that address. Since myptr points to x, *myptr prints the value 10. A reference is an alias or alternative name for an existing variable.
🌐
Unstop
unstop.com › home › blog › difference between pointer and reference (+examples)
Difference Between Pointer And Reference (+Examples)
February 4, 2025 - Then we declared a reference to temp and named it 'a'. In the next line, when we print the value of 'a', it shows the value of the variable where it is referencing. A pointer is a programming language object that stores the address of another variable. In other words, it stores the direct address of the memory location.
🌐
Standard C++
isocpp.org › wiki › faq › references
References, C++ FAQ
In other words, a C programmer will think of i as a macro for (*p), where p is a pointer to x (e.g., the compiler automatically dereferences the underlying pointer; i++ is changed to (*p)++; i = 7 is automatically changed to *p = 7). 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.
🌐
Codecademy
codecademy.com › learn › learn-c-plus-plus › modules › learn-cpp-references-and-pointers › cheatsheet
Learn C++: References & Pointers Cheatsheet | Codecademy
In C++, a reference variable is an alias for another object. It is created using the & sign. Two things to note: Anything done to the reference also happens to the original.
🌐
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.
Top answer
1 of 2
19

The above will be able to modify not only the pointed object, but also the pointer in itself. As an example, consider the below code:

void func(int*& ptr) {
    *ptr = 1;
    ptr = 0;
}

int main() {
    int x = 0;
    int* y = &x;
    func(y);
}

At the end of the execution, x has value 1 and y is 0 (as you can see).

Notice that for the sake of the example I've used 0 as a null pointer, but if you are using C++11 you should probably use nullptr instead (which does not have an overloaded operaror<< for std::ostream).


This concept can possibly be assimilated by taking a look at the following code:

template<class Type> using ptr = Type*;
ptr<int>& x;

or

std::unique_ptr<int>& x;

In these examples, x is a reference to a type (ptr<int> and then std::unique_ptr<int>), which just so happens to be a pointer/class with pointer semantic (operator* and operator->).


A possibly interesting digression can be made on the position of a const qualifier in the pointer. Consider these two instances:

  1. void func(const int*& ptr)
  2. void func(int*const& ptr)

Their meanings are:

  1. pointer by reference to a constant int.
  2. pointer by constant reference to an int.

And following the above analogy with ptr they would be:

  1. ptr<const int>&
  2. ptr<int> const&

Therefore the first will fail to execute *ptr = 1 in the body of the function (because the int is constant), but will happily execute ptr = 0.

The second will behave conversely, allowing *ptr = 1 (because the pointed int is not constant), while disallowing ptr = 0 (because the pointer is constant).

Of course, in the case of:

void func(const int*const& ptr)

which in the ptr analogy would be ptr<const int> const&, both of them wouldn't be allowed.


And when to use this? Is it advisable to do this?

Like every feature, you'll find its usefulness when you'll need it. But just as a general idea, some people used it to reset the pointer after freeing a dynamically allocated resource (I'm not recommending this, see below).

Let's take this example:

free_my_int(int*& ptr) {
    delete ptr;
    ptr = nullptr;
}

int* x = new int(42);
free_my_int(x);

At the end of the execution, x would be correctly freed and the pointer automatically set to nullptr (null pointer). This was done to prevent ugly segmentation faults or "pointer freed has not been allocated" error messages caused by a missing ptr = nullptr.

But with C++11 and C++14 there is very little use of pointers and even less of reference to pointers. Most of the things pointers where used for are not replaced with other standard construct (see std::optional, std::unique_ptr, std::shared_ptr or std::reference_wrapper for example).

2 of 2
2

Let's compare all three options:

  • void func(type* param); // 'param' is a 'type*' variable
  • void func(type& param); // 'param' is a reference to a 'type' variable
  • void func(type*& param); // 'param' is a reference to a 'type*' variable

void func(type* param)
{
    type x;
    ...
    param = &x;
    // Argument 'param' is regarded as a local variable in this function,
    // so setting 'param = ...' will have no effect outside this function
}

Of course, if you do *param = ..., then it will effect the contents of the memory pointed by param. And you can also do param[5] = ... for example, and effect other areas within that memory space.


void func(type& param)
{
    type x;
    ...
    param = x;
    // Argument 'param' is regarded as a reference to a variable outside this
    // function, so setting 'param = ...' will effect the referenced variable
}

Here, you will only change the referenced variable itself, so it's safer than declaring the function as void func(type* param), and then pass the address of type param by calling func(&param).


void func(type*& param)
{
    type x;
    ...
    param = &x;
    // Argument 'param' is regarded as a reference to a variable outside this
    // function, so setting 'param = ...' will effect the referenced variable
}

This is similar to declaring the function as void func(type** param), and then pass the address of type* param by calling func(&param), but again, it is safer for the same reason mentioned above.


🌐
Medium
kevin-yang.medium.com › c-pass-by-value-pointer-reference-ddc3780d907c
C++ Pass by Value, Pointer*, &Reference | by Kevin Yang | Medium
May 6, 2019 - A pointer is a special type of object that has the memory address of some object. The object can be accessed by dereferencing (*) the pointer, which knows the type of object it is pointing to.
🌐
UTK
web.eecs.utk.edu › ~bmaclenn › Classes › 102-S10 › thinkCScpp › chap16.htm
Chapter 16: Pointers and References
You have already learned that spacing in C++ does not matter, so the following pointer declarations are identical: SOMETYPE* somevar; SOMETYPE * somevar; SOMETYPE *somevar; The following reference declarations are identical as well: SOMETYPE& somevar; SOMETYPE & somevar; SOMETYPE &somevar; Different programmers write the declarations in different ways. In many respects, it makes more sense to think of the * or & as going with the type.
🌐
Cplusplus
cplusplus.com › forum › beginner › 261042
When to use pointers and references? - C++ Forum
August 30, 2019 - Now, one of the things you didn't ask about is implied with: void f( std::unique_ptr<A> & p ); That might not strike you, so look at the rawer version of it void f( A *& p ); That's a reference to a pointer. 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.