Answer as soon as possibe please; Linked list: I've a small question

Feb 20, 2013 at 4:44pm
If the prototype of my function is void sell_item(store **head,store **tail). In main i've passed sell_item(&head,&tail), where head & tail where both pointers of store(struct) type.

Now I want to access the the content where head is pointing to. How shall I access it? Like *head->next??? But this is not working.
where next is another member of store storing pointing to the next location of memory (pointing to next node).
Feb 20, 2013 at 5:08pm
Your function logically does not make sense. Sell what item? To who? How?
Feb 20, 2013 at 5:08pm
The question you should be asking is why is the prototype of your function accepting a pointer to a pointer to a struct or type store?

If head and tails are pointers to store ( aka Store * head, *tail ) don't you think your prototype should reflect that?
void sell_item(store * head, store * tail) ?

That way you could access the information in head & tail like so..

1
2
head->member1 = 30;
tail->member1 = 31;
Feb 20, 2013 at 5:12pm
Currently, you'd have to use (*head)->next
Feb 20, 2013 at 5:48pm
closed account (D80DSL3A)
@georgewashere Passing a pointer by copy won't allow the pointers to be reassigned if the 1st(head) or last(tail) node are deleted.

The pointers must be passed either by reference, or by passing a pointer to each pointer. I prefer pass by reference because it simplifies the syntax of their usage in the function.
Pass by reference:
1
2
3
4
void sell_item(store*& head, store*& tail)// hey!! How do we know what to sell here (LB's point)?
{
    head = head->next;// 1st node deleted. head reassigned
}

Pass a pointer:
1
2
3
4
void sell_item(store** head, store** tail)
{
    *head =(*head)->next;// per LB
}


EDIT: Brain-fart correction
Last edited on Feb 20, 2013 at 5:51pm
Topic archived. No new replies allowed.