Passing references to pointers in C++
Your function expects a reference to an actual string pointer in the calling scope, not an anonymous string pointer. Thus:
string 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.
Difference between pointer to a reference and reference to a pointer
First, a reference to a pointer is like a reference to any other variable:
void 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.:
int 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.
How does reference to pointer exactly work in C++, and when do we need them (in the case of linked list)
You pass a pointer by reference for the same reason you pass a non-pointer by reference: To let the function modify its value.
Let me use a simpler example
#include <iostream>
void foo(int*& x) {
*x = 42; // change the value of the int x points to
x = nullptr; // change the value of x
}
The first line modifies the value x
points to (but it does not modify x
). The second line modifies x
itself.
int main() {
int y = 42;
int* y_ptr = &y;
foo(y_ptr);
if (y_ptr == &y) std::cout << "cannot happen";
}
Because we set x = nullptr
, y_ptr
will not point to y
anymore after the call.
Now if we modify foo
to not take a reference we get:
#include <iostream>
void foo(int* x) {
*x = 42; // change the value of the int x points to
x = nullptr; // change the value of x
}
Again the first line modifies the int
pointed to by x
. However, now the second line only has an effect on x
local to the function.
int main() {
int y = 42;
int* y_ptr = &y;
foo(y_ptr);
if (y_ptr == nullptr) std::cout << "cannot happen";
}
The value of y_ptr
cannot change by passing it to foo
, because it is passed by value.
In your code you have
Node* deleteHead(Node* &head)
{
if (head)
{
Node* temp = head;
head = head->next;
delete temp;
}
return head;
}
And when you write head = deleteNode(head)
two things are happening:
- the function modifies
head
(because it is passed by reference) to point tohead->next
. - the function also returns this "new" head (pointing to
head->next
) and that is assigned tohead
.
So you basically asign to head
twice. Because head
is passed by reference deleteNode
would do the right thing without using the return value:
deleteNode(head); // this already does modify head
...or put the other way around: If you return the "new" head (head->next
) from the fucntion and assign it to head
, then it does not matter if you pass the pointer by reference, because the assignment done inside the function has the same effect.
Your code is similar to
int* bar(int*& x) {
x = nullptr;
return x;
}
and then call it via
int y = 42;
int* y_ptr = &y;
y_ptr = bar(y_ptr);
where the same effect could be achieved by not using the returned value bar(y_ptr)
. Or the same without pointers (because pointers really make no difference here):
int moo(int& x) {
x = 0;
return x;
}
int x = 42;
x = moo(x); // same as `moo(x)`
PS: You dont need both (return the pointer and assign it already in the function), so better make the function return void
.
Difference between pointer-to-pointer vs reference-to-pointer (C++)
The first example is that of a reference to a pointer, ie. a reference to a type IEnumWbemClassObject*
:
HRESULT Query ( IN BSTR sQuery, OUT IEnumWbemClassObject* &pEnumerator );
Therefore if pEnumerator
is declared as a IEnumWbemClassObject*
(which I'm assuming it is), you don't need to explicitly pass the address of pEnumerator
to the function or dereference the variable inside the function in order to change where pEnumerator
points (which would otherwise be required with an argument of IEnumWbemClassObject**
).
A reference to a pointer has the same behaviour as a reference to any other type, just think of the above example as being a "reference to a pointer" and not a "pointer to a reference." There's no such thing as a pointer to a reference.
Related Topics
How to Pause a Pthread Any Time I Want
How to (Un)Escape Strings in C/C++
Load the Same Dll Multiple Times
Is There a Name for This Tuple-Creation Idiom
Windows C++ Compiler with Full C++11 Support (Should Work with Qt)
How to Play or Open *.Mp3 or *.Wav Sound File in C++ Program
Is There Any Lame C++ Wrapper\Simplifier (Working on Linux MAC and Win from Pure Code)
How to Count Clock Cycles with Rdtsc in Gcc X86
How Many Spaces for Tab Character(\T)
Loop Until Integer Input Is in Required Range Fails to Work with Non-Digit Character Inputs
How to Have Two Functions That Call Each Other C++
Casting Double Array to a Struct of Doubles
What Exactly Is Streambuf? How to Use It
Returning Const Reference to Local Variable from a Function
Object Layout in Case of Virtual Functions and Multiple Inheritance