Thread: I don't understand this line in a linked list

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195

    I don't understand this line in a linked list

    The following code snippet is from:

    http://www.eskimo.com/~scs/cclass/int/sx8.html

    The code deletes a node from a linked list:

    Code:
    struct list **lpp;
    	for(lpp = &list; *lpp != NULL; lpp = &(*lpp)->next)
    		{
    		if((*lpp)->item == i)
    			{
    			*lpp = (*lpp)->next;
    			break;
    			}
    		}
    	}

    The part of the construction I don't really understand is

    Code:
    lpp = &(*lpp)->next
    Can someone break this part piece by piece? Like explain why the & operator is necessary. Why we just can't do something like
    lpp->next, etc.

    Thanks
    Chad

  2. #2
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    lpp is a pointer to a pointer to struct list, but presumably the next pointer is just a pointer to struct list, so the first thing that needs to be done is be able to get to the members by dereferencing the first pointer, thus (*lpp). Then you can use the arrow operator to get to next, (*lpp)->next. But because you're assigning the result to lpp, and lpp has an extra level of indirection, you need to get the address of the next pointer to add that level of indireciton, so you have &(*lpp)->next.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Ugghhh...... The concept of adding an extra layer of indirection. Another one of those quality topics that I'm struggling to understand.

    I really thought it got easier after grasping pointers and data types.
    Thanks

  4. #4
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    >I really thought it got easier after grasping pointers and data types.
    Not really. It's like math. Once you grasp arithmetic they throw algebra at you, and once you finally get algebra, you get smacked in the head with calculus. The more you learn, the more you're expected to be able to handle, and pointers are no different.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  2. Replies: 5
    Last Post: 11-04-2006, 06:39 PM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  4. Searching a linked list for char
    By spentdome in forum C Programming
    Replies: 3
    Last Post: 05-22-2002, 11:11 AM
  5. 1st Class LIST ADT
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2001, 07:29 PM