Help - What does litem*& list stands for?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

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

  1. #1
    Join Date
    Apr 2010
    Posts
    3

    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~

  2. #2
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    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.

  3. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,317

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

    Quote Originally Posted by Russco View Post
    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

  4. #4
    Join Date
    Apr 2010
    Posts
    3

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

    Quote Originally Posted by Russco View Post
    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?

  5. #5
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

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

    Quote Originally Posted by EugeneYu View Post
    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.

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

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

    Quote Originally Posted by EugeneYu View Post
    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?

  7. #7
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,145

    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.

  8. #8
    Join Date
    Apr 2010
    Posts
    3

    Thumbs up 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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center