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).

Dani AI

Generated

Short clarification and a couple of safe patterns that follow on from ’ explanation.

Because the function receives pointers-to-pointers, the first step inside the function is to dereference once to get the actual node pointer, then use that node pointer to access members. The common mistake comes from operator precedence: the member-access operators bind more tightly than the unary *, so the dereference must be done explicitly (or copied into a local pointer) before touching members. Also always check for null before dereferencing.

Example using a local pointer (keeps the double-indirection visible and explicit):

void sell_item(store **headPtr, store **tailPtr) {
    store *firstNode = *headPtr;
    if (firstNode == nullptr) return;    // empty list
    store *nextNode = firstNode->next;
    *headPtr = nextNode;                 // update head when removing first
    delete firstNode;                    // or free(), depending on allocation
}

C++-style alternative (simpler syntax inside the function):

void sell_item(store*& head, store*& tail) {
    if (head == nullptr) return;
    store *node = head;
    // work with node->next, update head/tail directly
}

Notes: when removing the last element, update tail appropriately; always null-check before dereference; prefer delete vs free consistent with allocation. For modern C++ prefer standard containers or smart pointers (std::forward_list, std::unique_ptr) to avoid manual memory and pointer-to-pointer bookkeeping.

Recommended Answers

All 2 Replies

head is a pointer to a pointer to a store.

So *head is a pointer to a store.

So *(*head) is a store.

So (*(*head)).next is the member variable next of the store.

Thanks dude.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.