If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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 ?

Re: Help - What does litem*& list stands for?

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.

Re: Help - What does litem*& list stands for?

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.

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.

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.

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?

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: