Help - What does litem*& list stands for?
I encountered a problem recently which requires me to implement a function with such signature:
int remove_consecutive_duplicates( linknode*& list );
The linknode here refers to a predefined structure:
struct linknode{
char data;
linknode* next;
};
Well, my question is, what does the notation - linknode*& list - stand for?
I got confused with *& being used together, my initial guess will be it is a pointer of type linknode, and it points to the address of object linknode, but not quite sure whether the understanding is correct.
If the above assumption is correct, what is the difference with - linknode* list ?
Guys, please help me on this if you got any idea, many thanks~
Re: Help - What does litem*& list stands for?
linknode* = pointer to linknode
linknode& = reference to linknode
therefore...
linknode*& = reference to a pointer to linknode.
The reason that the pointer is passed like this is because the function will have to change the pointer and have those changes propagate to the caller of the function.
Re: Help - What does litem*& list stands for?
Quote:
Originally Posted by
Russco
linknode* = pointer to linknode
linknode& = reference to linknode
therefore...
linknode*& = reference to a pointer to linknode.
The reason that the pointer is passed like this is because the function will have to change the pointer and have those changes propagate to the caller of the function.
also,
linknode&* = pointer to a reference linknode.
This does not exist, and will not compile
Re: Help - What does litem*& list stands for?
Quote:
Originally Posted by
Russco
linknode* = pointer to linknode
linknode& = reference to linknode
therefore...
linknode*& = reference to a pointer to linknode.
The reason that the pointer is passed like this is because the function will have to change the pointer and have those changes propagate to the caller of the function.
Thanks Russco. Well, what confused me is, i can simply use linknode*, which is a pointer to the first linknode, and any changes inside the function will also be propagated to the caller of the function.
so why do we need to use linknode*& here?
Re: Help - What does litem*& list stands for?
Quote:
Originally Posted by
EugeneYu
Thanks Russco. Well, what confused me is, i can simply use linknode*, which is a pointer to the first linknode, and any changes inside the function will also be propagated to the caller of the function.
so why do we need to use linknode*& here?
No they wont.
All parameter passing in C is done by value, this includes pointers. C++ also does this too to retain backward compatibility with C, but also adds reference types which C doesn't have. In C reference types were simulated by a pointer to a pointer.
The pointer passed without the reference symbol is copied into the function, so any changes imposed are on a copy of the pointer. When you use the reference symbol, the pointer is not copied into the function. Now any changes within the function to the pointer will propagate to the caller as the function is not working on a copy.
See this code sample
Code:
#include <iostream>
using namespace std;
void byval( int* ptr )
{
++ptr;
}
void byref( int*& ptr)
{
++ptr;
}
int main()
{
int* ptr = 0;
cout << " ptr = " << ptr << endl;
byval( ptr );
cout << " ptr after byval = " << ptr << endl;
byref( ptr );
cout << " ptr after byref = " << ptr << endl;
return 0;
}
Re: Help - What does litem*& list stands for?
Quote:
Originally Posted by
EugeneYu
Thanks Russco. Well, what confused me is, i can simply use linknode*, which is a pointer to the first linknode, and any changes inside the function will also be propagated to the caller of the function.
so why do we need to use linknode*& here?
You're confused because changes to what the pointer points to will be propagated. However, changes to the pointer itself will not, and that's a problem----what if the first node is one of the ones which needs to be removed?
Re: Help - What does litem*& list stands for?
In addition to Russco's explanation, if you wanted the C equivalent of
Code:
void set(int*& ptr)
{
ptr = assign_to_something();
}
then you would need to write
Code:
void set( int** ptr)
{
*ptr = assign_to_something();
}
which not only makes the function look slightly messier, but from an interface perspective, it is not obvious why you require int** ptr - a user may legitimately ask, is it because you want to modify the pointer or, is it that you require int** prt because you are expecting a pointer to an array of pointers? In addition, the caller needs to reference the pointer in the function call i.e.
Code:
int main()
{
int* ptr=0;
set(&ptr);
}
The pass-by-referece mechanism that C++ offers is much clearer and leaves far less room for error:
Code:
void set(int*& ptr)
{
ptr = assign_to_something();
}
int main()
{
int* ptr=0;
set(ptr);
}
Re: Help - What does litem*& list stands for?
Thanks Russco, LindLey and PredicateNormative.
Now I am much more clear on this.
linknode*& means we can change the pointer value and pass back to caller function, like point to another linknode.
linknode* - the change on pointer value won't be passed back to caller function, but we still can use it to change the data which the pointer points to.
And I also have just finished implementing that function =)