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

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).
Your function logically does not make sense. Sell what item? To who? How?
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;
Currently, you'd have to use (*head)->next
@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
Topic archived. No new replies allowed.