Page 1 of 1

## 4 Replies - 409 Views - Last Post: 21 June 2012 - 12:51 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=283498&amp;s=99124e2410ce7af63d030c8de001008d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jakes99

Reputation: -10
• Posts: 4
• Joined: 21-June 12

Posted 21 June 2012 - 12:05 PM

```main()
{
/*function call*/
start=del(st, data);}
```

insert
```struct node *addatbeg(struct node *start, int data)
{
struct node *tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=data;
start=tmp;
return start;
}
```

delete
```struct node *del(struct node *start, int data)
{
struct node *tmp;
if(data == start->info)
{
tmp = start;
free(tmp);
return start;
}
}
```

QUESTION:
start will have its own address suppose that address is 11d and start is a pointer that will store an address of the first node suppose the address of that first node is 100c.
Now look at the delete function. if we delete the first node.
tmp = start; this means that tmp is a pointer which will store the address of start address of start and address of start is 11d

this means pointer start will now store the address of the next node. address of start is 11d. and in 11d memory location it should store the address of new node

Now we do free(tmp);
tmp contains the address of start which is 11d... if we are freeing 11d memory location then shouldn't all its data be erased in the previous step we assigned start=start->link so it should also have got erased..
Whats happening ?

This post has been edited by NickDMax: 21 June 2012 - 12:09 PM

Is This A Good Question/Topic? 0

### #2 NickDMax

Reputation: 2254
• Posts: 9,245
• Joined: 18-February 07

Posted 21 June 2012 - 12:10 PM

This looks like a homework question to me.

### #3 jakes99

Reputation: -10
• Posts: 4
• Joined: 21-June 12

Posted 21 June 2012 - 12:15 PM

NickDMax, on 21 June 2012 - 12:10 PM, said:

This looks like a homework question to me.

think you didn't understand the question

jakes99, on 21 June 2012 - 12:11 PM, said:

NickDMax, on 21 June 2012 - 12:10 PM, said:

This looks like a homework question to me.

think you didn't understand the question

its using pointer to pointer. I am bit confused in understanding pointer to pointer thats what causing the confusion

### #4 jakes99

Reputation: -10
• Posts: 4
• Joined: 21-June 12

Posted 21 June 2012 - 12:40 PM

NickDMax, on 21 June 2012 - 12:10 PM, said:

This looks like a homework question to me.

This is just one of those guys who think they only know every thing and everyone else is a lamer. But this guy is the greatest dumb ass in the world..

This question was totally made my me and i have figured out the answer. The concept of double pointers is being used.
suppose the address of start is 11d and the address of st which is in main function is 5b .. when you are calling from main function you are assigning start = st through parameters.

Now we do tmp = start
Now tmp is a double pointer so it will not store 6b instead it will store 5b.
start = tmp->link .. address of new node assigned to start ..the address of start is 6b

free(tmp);
now temp is storing address 5b .. 5b actually the address of the node which is to be delete ie; first node so we are freeing that 5b memory location.

return start;
because start has the address of new node now we will return it to main function

This post has been edited by JackOfAllTrades: 21 June 2012 - 01:00 PM

### #5 jimblumberg

Reputation: 4892
• Posts: 15,280
• Joined: 25-December 09

Posted 21 June 2012 - 12:51 PM

POPULAR

That's enough of this. Topic closed.

Jim