473,386 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Double pointers!

I have written a code of deleting a node from a link list using double pointers,i am not sure if i'll be able to explain what i want to ask..
so the code goes like this:
Expand|Select|Wrap|Line Numbers
  1. node **ptr=NULL;
  2. node **delete(int value)
  3. {
  4.     int flag=0;
  5.     node **temp=&root;
  6.     while(*temp!=NULL)
  7.     {
  8.         if((*temp)->value==value)
  9.         {
  10.             flag =1;
  11.             break;
  12.         }
  13.         else
  14.             {
  15.               temp=&(*temp)->next;
  16.               printf("In else: temp=%p  *temp=%p\n",temp,*temp);
  17.             }
  18.     }
  19.     if(flag==1)
  20.         {
  21.             printf("\nIn **delete()value found.Returning &temp=%p value=%d\n",&temp,(*temp)->value);
  22.  
  23.             return temp;
  24.         }
  25.     else
  26.         printf("\nNot found\n");
  27. }
  28.  
  29. void main()
  30. {
  31. int loop=0;
  32. node *temp;
  33. addNodes(1);
  34. addNodes(2);
  35. addNodes(3);
  36. addNodes(4);
  37. addNodes(5);
  38.  
  39. display();
  40. ptr=delete(3);
  41. *ptr=(*ptr)->next;
  42. display();
  43.  
  44. }
  45. This code works as reqd,deletes node with value 3.
  46. Here's the Output:
  47.  
  48. *ptr:0x900f008 and value:1 Pointing to:0x900f018
  49. *ptr:0x900f018 and value:2 Pointing to:0x900f028
  50. *ptr:0x900f028 and value:3 Pointing to:0x900f038
  51. *ptr:0x900f038 and value:4 Pointing to:0x900f048
  52. *ptr:0x900f048 and value:5 Pointing to:(nil)
  53.  
  54. In else: temp=0x900f00c  *temp=0x900f018
  55. In else: temp=0x900f01c  *temp=0x900f028
  56.  
  57. In **delete()value found.Returning &temp=0xbff9a948 value=3
  58.  
  59. *ptr:0x900f008 and value:1 Pointing to:0x900f018
  60. *ptr:0x900f018 and value:2 Pointing to:0x900f038
  61. *ptr:0x900f038 and value:4 Pointing to:0x900f048
  62. *ptr:0x900f048 and value:5 Pointing to:(nil)
  63.  
The step *ptr=(*ptr)->next; is causing confusion.
value 2 is pointing to add 0x..28
value 3 is pointing to add 0x..38
value 4 is pointing to add 0x..48
3 was to be deleted which was at location 0x..28,whatever operation i did was at the location 0x..28(*ptr=(*ptr)->next),i didnt updated add 0x..18 to point to 0x..38 instead of 0x..28,then how it happened?

Basically i am getting confused what is happening via double pointer.If at 0x..28 location i am just copying next locations contents etc,then location 28 should have been retained.If i am loosing the track of 0x..28 then how 18 knew to point correctly to the next location?


I hope i am somewhat clear.
Mar 22 '11 #1
6 2616
weaknessforcats
9,208 Expert Mod 8TB
ptr is a pointer to a pointer to a node:

Expand|Select|Wrap|Line Numbers
  1. node **ptr;
Therefore, *ptr is a pointer to a node.

Therefore, (*ptr)-> next is the value of the member next in the node.

Therefore, *ptr = (*ptr)->next assigns the address in the next member of the node whose address is pointed at by ptr. This is a way to move down the list when you have the address of a node pointer.
Mar 22 '11 #2
I agree, what's happening in that line is similar to:
Expand|Select|Wrap|Line Numbers
  1. node *current
  2. current=current->next
means exactly whats happening at *ptr=(*ptr)->next;
But using single pointer to the node,if you would have done
current=current->next,it wont effect the link list at all,it will remain as it is,but using double pointer this line
*ptr=(*ptr)->next is actually deleting the node.

How it is traversing is clear,but problem is,what i am returning from the delete function is the address of (the address of (the node to be deleted)) and then i am performing the above operation,so what is happening at the address of the to be deleted is the problem..
Mar 23 '11 #3
weaknessforcats
9,208 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. node **temp=&root; 
  2.     while(*temp!=NULL) 
  3.     { 
  4.         if((*temp)->value==value) 
  5.         { 
  6.             flag =1; 
  7.             break; 
  8.         } 
  9.         else 
  10.             { 
  11.               temp=&(*temp)->next; 
  12.               printf("In else: temp=%p  *temp=%p\n",temp,*temp); 
From your code, temp has the address of the root. The root contains the address of a node.

It looks like you have one too many layers of indirection. I think you could code:

Expand|Select|Wrap|Line Numbers
  1. node* temp = root;
instead of:

Expand|Select|Wrap|Line Numbers
  1. node** temp = & root.
Then when you code:

Expand|Select|Wrap|Line Numbers
  1. temp=&(*temp)->next; 
you cold change that to:

Expand|Select|Wrap|Line Numbers
  1. temp = temp->next;
In any case, you are not deleting a node. All you are doing is returning when you find the node to delete but you are not actually deleting it.

If your list is A B C then when you delete B you must change A->next to C. That means whe you get to B in your delete function you still need to know the address of the node before B. Then you set the next of that node to the next of B.
Mar 23 '11 #4
I am retunrning temp back to main(),where i am holding this returned value in another node **ptr.,and then i am doing *ptr=(*ptr)->next;
Ok,i'll change the code a little bit,i am not returning temp any more.Now my delete function's protoype will be :
Expand|Select|Wrap|Line Numbers
  1. void delete(int value)
  2. {
  3. ..
  4. ..same as before..
  5.  if(flag==1)
  6.          {
  7.             *temp=(*temp)->next;
  8.          }
  9. }
this will also delete the required node.
As i have said earlier,i fully agree with you that if we'll take single layer pointer,it can be done via temp=temp->next; But using double pointer i am able to delete that node somehow using the above line
Mar 23 '11 #5
I am attaching the whole code,please somebody run it yourself and tell me about this issue.
In function delete,*ptr=(*ptr)->nextis deleting the node successfully,confusing part is not the traversing or what i am doing,but what is happening at the address of the node to be deleted



Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. typedef struct node{
  5. int value;
  6. struct node *next;
  7. }node;
  8.  
  9.  
  10. node *root=NULL;
  11. node **ptr=&root;
  12.  
  13. node * getNode(int val)
  14. {
  15.     node *temp=malloc(sizeof(node));
  16.  
  17.     temp->value=val;
  18.     temp->next=NULL;
  19.     return temp;
  20.  
  21. }
  22.  
  23.  
  24.  
  25. void swap(node **ptr)
  26. {
  27.     node *temp;
  28.     temp=((*ptr)->next)->next;
  29.     ((*ptr)->next)->next=*ptr;
  30.     *ptr=(*ptr)->next;
  31.     ((*ptr)->next)->next=temp;
  32.  
  33.  
  34. }
  35.  
  36. void sort()
  37. {
  38.     ptr=&root;
  39.     int value1,value2;
  40.     while(((*ptr)->next)!=NULL)
  41.     {
  42.         value1=(*ptr)->value;
  43.         value2=(((*ptr)->next)->value);
  44.         if(value1>value2)
  45.         {
  46.             swap(&(*ptr));
  47.             ptr=&root;
  48.         }
  49.         else
  50.             ptr=&(*ptr)->next;
  51.     }
  52.  
  53. }
  54.  
  55.  
  56.  
  57. node addNodes(int value)
  58. {
  59.  
  60.  
  61.     if(root==NULL)
  62.         root=getNode(value);
  63.     else
  64.     {
  65.         (*ptr)->next=getNode(value);
  66.         ptr=&(*ptr)->next;
  67.  
  68.     }
  69.  
  70.  
  71. }
  72.  
  73. void display()
  74. {
  75.  
  76. printf("\n");        
  77.     ptr=&root;
  78.     while(*ptr!=NULL)
  79.     {
  80.         printf("*ptr:%p and value:%d Pointing to:%p\n",*ptr,(*ptr)->value,(*ptr)->next);
  81.         ptr=&(*ptr)->next;
  82.     }
  83. printf("\n");        
  84. }
  85.  
  86.  
  87.  
  88.  
  89. node **delete(int value)
  90. {
  91.     int flag=0;
  92.     node **temp=&root;
  93.     while(*temp!=NULL)
  94.     {
  95.         if((*temp)->value==value)
  96.         {
  97.  
  98.  
  99.             flag =1;
  100.             break;
  101.         }
  102.         else
  103.             {
  104.             temp=&(*temp)->next;
  105.             printf("In else: temp=%p  *temp=%p\n",temp,*temp);
  106.             }
  107.     }
  108.     if(flag==1)
  109.         {
  110.  
  111.             printf("\nIn **delete()value found.Returning &temp=%p value=%d\n",&temp,(*temp)->value);
  112.  
  113.             return temp;
  114.         }
  115.     else
  116.         printf("\nNot found\n");
  117. }
  118.  
  119.  
  120. node *delete2(int value)
  121. {
  122.     int flag=0;
  123.     node *temp=root;
  124.     while(temp!=NULL)
  125.     {
  126.         if((temp)->value==value)
  127.         {
  128.             flag =1;
  129.             break;
  130.         }
  131.         else
  132.             temp=temp->next;
  133.     }
  134.     if(flag==1)
  135.         {
  136.             printf("In Delete:value Found &temp:%p\n",&temp);
  137.             return temp;
  138.         }
  139.     else
  140.         printf("\nNot found\n");
  141. }
  142. void main()
  143. {
  144. int loop=0;
  145.  
  146. node *temp;
  147. addNodes(1);
  148. addNodes(2);
  149. addNodes(3);
  150. addNodes(4);
  151. addNodes(5);
  152.  
  153. display();
  154.  
  155. ptr=delete(3);
  156. *ptr=(*ptr)->next;
  157.  
  158. display();
  159.  
  160. }
  161.  
  162.  
Mar 25 '11 #6
weaknessforcats
9,208 Expert Mod 8TB
How does

Expand|Select|Wrap|Line Numbers
  1. *ptr=(*ptr)->next; 
delete a node? I mean the node was allocated with malloc but nowhere do I see a free() of that allocation.

The delete should occur inside the delete function. That is, if temp is the node to delete, then:

Expand|Select|Wrap|Line Numbers
  1. savetemp->next = temp->next;
  2. free(temp);
where savetemp is the address of the node before temp (which you saved in the "if" statement inside the traverse loop).
Mar 25 '11 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: junky_fellow | last post by:
Do all double pointers have same size on a particular implementation. eg char **c_ptr; int **i_ptr; Is the size of c_ptr and i_ptr always same (on same implementation) or it may be different...
18
by: agni.suresh896 | last post by:
Hi i am currently working on Snmp related project. I want to know deep concepts of "pointers" so if any one provide me information related to datastructures and pointers i am very happy. Thanks...
2
by: warnockg90 | last post by:
Hi. I am trying to write a program that utilises pointers to dynamically allocate memory. Initially I used arrays to run the program but this is not so good when you have user inputted values and...
2
by: Bhan | last post by:
Hi, Can I get answers for the following: 1. Usage of Double Pointers during allocation memory.Heard that if memory is to be allocated by someone who is working on another module,then we...
12
by: Extremist | last post by:
Hi there I program in Linux, C++. If you declare a char pointer in C++ , you would say char *pointer; And if you want to allocate memory for that pointer you would say
0
by: Keith Thompson | last post by:
jameskuyper@verizon.net writes: In addition to the advice James gave you, please avoid the term "double pointer" to refer to a pointer to a pointer. A "double pointer" would be a pointer to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.