Thread: passing pointer vs passing address of pointer

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    7

    passing pointer vs passing address of pointer

    I have implemented a linked list, and while doing so I was encouraged to be aware of the difference between passing Node* head, and Node*& head. I have experimented a bit and often times the function still does the same thing regardless of which I use. For example the print function runs the same both ways. I am just looking for a better understanding, I have studied pointers (although I am still obviously rather new to them) but I can't seem to get my head around which to use and where.

    Code:
    void printLL(Node* head) {
       while (head != 0) {
          cout << head->key << " ";
          head = head->next;
       }
    }
    
    // compare to 
    
    void printLL(Node*& head) {
       while (head != 0) {
          cout << head->key << " ";
          head = head->next;
       }
    }
    Thank You

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    In the first case the modification of head inside the loop does not modify what head points to outside the loop. In the second case, when the function exits you will actually have modified what the pointer used as argument points to.

    Run the following and you will see that at "THIRD *p" you will have changed p in main to point to global2.
    Code:
    #include <iostream>
    int global1 = 10, global2 = 20;
    
    void printLL(int* p) {
        std::cout << "Pointer: " << *p << std::endl;
        p = &global2; // This has no effect
    }
    
    // compare to
    
    void printLL2(int*& p) {
        std::cout << "Reference to pointer: " << *p << std::endl;
        p = &global2; // p in main will now continue to point to global2
    }
    
    int main()
    {
        std::cout << "global1: " << global1 << std::endl;
        std::cout << "global2: " << global2 << std::endl;
    
        int *p = &global1;
        std::cout << "FIRST *p: " << *p << std::endl;
        printLL(p);
        std::cout << "SECOND *p: " << *p << std::endl;
        printLL2(p);
        std::cout << "THIRD *p: " << *p << std::endl;
    }
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,416
    Quote Originally Posted by googol
    passing pointer vs passing address of pointer
    (...)
    I was encouraged to be aware of the difference between passing Node* head, and Node*& head
    In neither case do you pass the address of the pointer. In the former, you pass the pointer by value. In the latter, you pass the pointer by reference, or we can say that a reference parameter was used. It may be the case that to implement this aliasing the compiler uses what amounts to a pointer to the pointer, but that is up to the implementation.

    Quote Originally Posted by googol
    have experimented a bit and often times the function still does the same thing regardless of which I use. For example the print function runs the same both ways.
    You will notice the difference should you modify the parameter. For example, call your print function twice in a row. You will find that with your Node* head version, the linked list will be printed twice. With your Node*& head version, the linked list will be printed once. This is because for the Node* version, assigning to head only changes the local copy of the pointer. For the Node*& version, head is an alias of the argument from the caller, so assigning to head locally also changes the original from the caller.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Take a look at this pointer tutorial. That should help clear some things up for you. Also if you are doing linked list implementation, this is a good resource.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-27-2014, 12:10 AM
  2. Passing address or pointer as argument to function
    By Edelweiss in forum C Programming
    Replies: 7
    Last Post: 08-17-2011, 12:38 AM
  3. Replies: 3
    Last Post: 06-06-2011, 03:46 AM
  4. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  5. Replies: 4
    Last Post: 11-05-2006, 02:57 PM