Thread: pointer to a pointer

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    10

    pointer to a pointer

    I need help in understanding some example code. If you want to delete the last node of the list L, why does the parameter have to be a pointer to a variable containing a node pointer? In other words why is it **L? Is there any list example that would not have its last node deleted if the parameter had been only a node pointer as in the function prototype void DeleteLastNode(NodeType *L)

    Code:
    void DeleteLastNode(NodeType **L)
    {
       NodeType *PreviousNode, *CurrentNode;
    
       if (*L!=NULL)
       {
          if ((*L)->Link == NULL)
          {    
             free(*L);
             *L=NULL;
          }
    
          else
          {
             PreviousNode=*L;
             CurrentNode=(*L)->Link;
    
             while (CurrentNode->Link!=NULL)
             {
                PreviousNode=CurrentNode;
                CurrentNode=CurrentNode->Link;
             }
    
             PreviousNode->Link=NULL;
             free(CurrentNode);
    
          }
       }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > In other words why is it **L?
    When there is only one node in the list, the last node is also the first node.
    Then you really need to update the pointer which points to the list.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User penney's Avatar
    Join Date
    Jan 2003
    Posts
    47
    If you had only passed a single pointer to the routine then the routine would have the address of the memory pointer only. By passing a double pointer you are getting the address of what the pointer points to and that is the memory that you are trying to alter.

    The only analogy that I could think of to help explain this is to think of it in terms of a post office and your home:

    A post office knows where your house is based upon your address. However, the post office is itself a location which has an address but obviously not your house. The post office is the pointer. Your address (where your actual house resides) is the data. I don't know if this elaboration helps at all or has confused you more.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM