-
April 21st, 2010, 07:40 AM
#1
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~
-
April 21st, 2010, 07:46 AM
#2
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.
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
-
April 21st, 2010, 07:52 AM
#3
Re: Help - What does litem*& list stands for?
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
-
April 21st, 2010, 08:24 AM
#4
Re: Help - What does litem*& list stands for?
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?
-
April 21st, 2010, 08:38 AM
#5
Re: Help - What does litem*& list stands for?
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;
}
Last edited by Russco; April 21st, 2010 at 08:49 AM.
Reason: added a small sample to illustrate better
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
-
April 21st, 2010, 09:10 AM
#6
Re: Help - What does litem*& list stands for?
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?
-
April 21st, 2010, 09:17 AM
#7
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);
}
Last edited by PredicateNormative; April 21st, 2010 at 09:19 AM.
-
April 22nd, 2010, 07:34 AM
#8
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 =)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|