   Double pointers
  Hi,I am trying to figure out how double pointers work. I can understand the basic reference/de-reference operators. But the following code has my head spinning for a while.This is loosely based from one of the tutorials i am following.#include <iostream>using namespace std;typedef struct Intstr{    int data;    struct Intstr * next;} inStr;int main(){    inStr *head;    inStr *node;    node = new inStr();    head = node;    node->data =1;    node->next = new inStr();    node = node->next;    node->data =2;    node->next = new inStr();    node = node->next;    node->data =3;    node->next = NULL;    inStr *link;    for(link = head; link !=NULL; link = link->next)    {   cout<<"Value of link is "<<(int)link->data<<endl;    }    inStr **del ;    for(del = &head;(*del)->next!=NULL;del = &(*del)->next )    {   //cout<<"Data is "<< (*del)->data<<endl;   if( (*del)->data == 2)   {  cout<<"This is node 2"<<endl;  *del = (*del)->next;  break;   }    }    for(del = &head;(*del)!=NULL;del = &(*del)->next )    {   cout<<"Data is "<< (*del)->data<<endl;    }    return 0;}A very basic implementation of a list. I do not understand the delete operation at all; it seems to work , but i am unable to understand how it achieves it.Here lies the problem : *del = (*del)->next;How does this break the queue to 2 elements ?Thanks.   
  Lets see if this helps.int var = 4;int *pVar = &varso, var is 4, and pVar points to var.Now *pVar is 4. Read this as 'contents of pvar'.Because pVar is pointer to an int, *pVar is getting that int. What you are doing is pointing to another pointer, so the contents of the pointer to the pointer, *del, is a pointer.Does that help at all?   ;  I find it sometimes clarifies things if you think of it in terms of a typedef, becase then there's only one pointer ;)typedef inStr* inStrPtr;inStrPtr* del;//a pointer to an inStrPtrIn your code, del stores the address of the links in your list. It starts off holding the address of the link to the head, then holds the address of each item's next member."*del = " changes the value of this link (whichever one del is pointing to)."(*del)->next" is the value of the next member."del = " changes which link (the address) del is pointing to."&(*del)->next" is the address of a next member.So on the first iteration, "*del = (*del)->next;" is the same as "head = head->next", which simply removes the head node from the list.On subsequent iterations, it's simmilar to "head->next = head->next->next", etc..This code is leaking memory by the by... It should probably look like:inStr* deleteMe = *del;*del = (*del)->next;delete deleteMe;;  Hmmm, ok here goes.A 'double pointer' is a pointer to a pointer, you can think of it like thisdouble_ptr -> ptr -> variablenote: the ASCII arrows here and after mean 'points to'dereferancing a pointer is just like taking it out of the picture and reading what it actually points to like this:*double_ptr 'reads as' ptr -> variableif you wanted to find the value at the end of the 'pointer chain' you need to dereferance it as many times as needed. In this case we are handling a double pointer.variable 'reads as' variable*ptr 'reads as' variable**double_ptr 'reads as' variableNow, lets think about what is happening in this snippet of code:inStr **del ;    for(del = &head;(*del)->next!=NULL;del = &(*del)->next )first a 'double pointer' is created called del we know this because it has 2 asterisk in front of its name.second we enter a for loop with the intialisation part initialising del to the address of head, remember head is a pointer to data so we need to provide an ampersand ('&') to the front of head as an added level of indirection. this ensures that del is initialised with the address of head and not what it points to otherwise this would be a compiler error since a 'double pointer' cannot point directly to data but instead must be indirect and point to a another pointer.the second part of the for loop is a little more complicated; think about what is happening here. First in brackets we have:(*del) 'reads as' headnow remember del points to head so deferencing  del will give us head. next we have:(*del)->next 'reads as' head->nextnow if you know pointers, you know that the '->' operator is used to access members of a class/struct and since we dereferanced del to head, which points to data as it is a 'single pointer' we can access the members of the data it points to through this operator.to add to confusion, the line:(*del)->next != NULLchecks next is not equal to NULL since next is ALSO a pointer inside the class/struct it pointed to. If you do not know about NULL pointers you should do some research on them other than that theres not much else to say on that part.The final part; another confusing piece of syntax, but lets break it down again like we did before.first we must do the brackets, so again:(*del) 'reads as' headnext we have:(*del)->next 'reads as' head->next 'or' nextnow something that is not similar to the second part of the for loop is the ampersand and the assignment to del. well we know that next is a pointer to data (in this case a pointer to a struct of the same type) so in order to allow del to point to next we must provide the ampersand for that added level of indirection as we discussed earlier, which gives us:del = &(*del)->nextordel = &nextnote: double pointers are rare and triple pointers even rarer I dont think ive ever seen one just because they become very unweildly quickly.Hope that helps, pointers (even 'singular' ones) are often a difficult concept for new comers (they were for me to) but you will get there in the end, you just need to use them. :)[Edited by - CodeCriminal on May 16, 2010 6:07:27 PM]